sabari k sabari k - 1 month ago 5
PHP Question

Assign different class for same div in while loop

I have code like below,

<?php
$sel_ms1= mysqli_query($conn,"select * from tbl_milestone where ms_status = 'Y' order by ms_pos asc");
while($sel_ms = mysqli_fetch_array($sel_ms1))
{
?>
<tr>
<td align="center"><span class="btn btn-success"><?php echo $sel_ms['ms_year']; ?></span></td>
<td class=''><?php echo $sel_ms['ms_desc']; ?></td>
</tr>
<?php
}
?>


And I want to assign different class for second td like,

<tr>
<td align="center"><span class="btn btn-success"><?php echo $sel_ms['ms_year']; ?></span></td>
<td class=''><?php echo $sel_ms['ms_desc']; ?></td>
</tr>
<tr>
<td align="center"><span class="btn btn-success"><?php echo $sel_ms['ms_year']; ?></span></td>
<td class='error'><?php echo $sel_ms['ms_desc']; ?></td>
</tr>
<tr>
<td align="center"><span class="btn btn-success"><?php echo $sel_ms['ms_year']; ?></span></td>
<td class=''><?php echo $sel_ms['ms_desc']; ?></td>
</tr>
<tr>
<td align="center"><span class="btn btn-success"><?php echo $sel_ms['ms_year']; ?></span></td>
<td class='warning'><?php echo $sel_ms['ms_desc']; ?></td>
</tr>


The above four will be looped respectively.
How can I achieve it through loop?

Answer

Please use :nth-child() Selector using jquery and you will get result.

$( "tr td:nth-child(2)" ).addClass( "secondclass" );

Comments