ale1981 Posted November 27, 2008 Share Posted November 27, 2008 I can not figure this out, what I want to do is alternate the row colour of a table depending on a variable when looping through results from a db query, example; ORD123 ORD123 ORD124 and so on.. <table> $trclass = 'alt1' $trclass = 'alt2' while ($ord_inf = mssql_fetch_array($result)) { if ($ordinf['ORDERNO'] == $orderNo) { 'keep same tr class as its the same order' } else { 'its a different order so change the tr class' } <tr class="$trclass"> <td>$ord_inf['ORDERNO']</td> </tr> $orderNo = $ord_inf['ORDERNO']; } </table> something like that, i just cant seem to get it to work! any help appreciated... [/] Quote Link to comment https://forums.phpfreaks.com/topic/134480-solved-alternate-row-colour-depending-on-variable/ Share on other sites More sharing options...
sasa Posted November 27, 2008 Share Posted November 27, 2008 try <table> <?php $trclass_array = array('alt1', 'alt2'); $orderNo = ''; $color = 1; while ($ord_inf = mssql_fetch_array($result)) { if ($ordinf['ORDERNO'] != $orderNo) { $color = 1 - $color; $orderNo = $ordinf['ORDERNO']; } $trclass = $trclass_array[$color]; //... etc } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/134480-solved-alternate-row-colour-depending-on-variable/#findComment-700266 Share on other sites More sharing options...
ale1981 Posted November 27, 2008 Author Share Posted November 27, 2008 Hi, thanks for your reply but all rows end up the same colour, using alt1. Quote Link to comment https://forums.phpfreaks.com/topic/134480-solved-alternate-row-colour-depending-on-variable/#findComment-700293 Share on other sites More sharing options...
ale1981 Posted November 27, 2008 Author Share Posted November 27, 2008 SOLUTION for anybody interested; <table> <?php while ($ord_inf = mssql_fetch_array($result)) { if($lastOrderNo != $ord_inf['ORDERNO'] || $lastOrderNo == "") { $trclass = ($trclass == 'alt2') ? 'alt1' : 'alt2'; } $lastOrderNo = $ord_inf['ORDERNO']; ?> <tr class="<?php echo $trclass; ?>"> <td><?php echo $ord_inf['ORDERNO']; ?></td> </tr> <?php } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/134480-solved-alternate-row-colour-depending-on-variable/#findComment-700326 Share on other sites More sharing options...
o3d Posted November 27, 2008 Share Posted November 27, 2008 Just my 2c using the modulus operator for ($i=0; $i<10; $i++) { if (($i % 2) == 0) { //even colors } else { //odd colors } } Quote Link to comment https://forums.phpfreaks.com/topic/134480-solved-alternate-row-colour-depending-on-variable/#findComment-700367 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.