Clinton Posted January 6, 2009 Share Posted January 6, 2009 How do I get every other result to highlight its background a different color? Here's me code: <table width="95%" border="0" cellspacing="0" cellpadding="1" class="boxtitle"> <?php $sql = "SELECT * FROM thelist WHERE username = '$uid' ORDER BY jpted"; $rs = mysql_query($sql); while($row = mysql_fetch_array($rs)) { extract($row); ?> <tr><td> <a href="editaj.php?jid=<?php echo $id; ?>"> <?php echo $jtitle; ?> </a></td> <td> <?php echo $city; ?>, <?php echo $state; ?> </td> <td><a href="editaj.php?jid=<?php echo $id; ?>"> Edit </a></td> <td><a href="editaj.php?jid=<?php echo $id; ?>&del=yes"> Delete </a></td> </td></tr> <?php } ?> </table> Link to comment https://forums.phpfreaks.com/topic/139743-solved-every-other-while-highlighting/ Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 <table width="95%" border="0" cellspacing="0" cellpadding="1" class="boxtitle"> <?php $sql = "SELECT * FROM thelist WHERE username = '$uid' ORDER BY jpted"; $rs = mysql_query($sql); $i=0; while($row = mysql_fetch_array($rs)) { extract($row); $bground = "some style"; if (($i%2) == 0) $bground = "somealretnatestyle"; $i++; ?> <tr class="<?php echo $bground; ?>"><td> <a href="editaj.php?jid=<?php echo $id; ?>"> <?php echo $jtitle; ?> </a></td> <td> <?php echo $city; ?>, <?php echo $state; ?> </td> <td><a href="editaj.php?jid=<?php echo $id; ?>"> Edit </a></td> <td><a href="editaj.php?jid=<?php echo $id; ?>&del=yes"> Delete </a></td> </td></tr> <?php } ?> </table> And a side note, the % operator is the modulus operator if you want to look up on it. http://us2.php.net/manual/en/language.operators.arithmetic.php Link to comment https://forums.phpfreaks.com/topic/139743-solved-every-other-while-highlighting/#findComment-731122 Share on other sites More sharing options...
Clinton Posted January 6, 2009 Author Share Posted January 6, 2009 Ok, so it does a/b or in this case $i/2. $i starts off as 0 so 0/2 = 0 and it's the first style. Then it adds one $i++ so then it's 1/2, which is different then 0. But then we add one more, right??? Which then becomes 2/2... which equals 1... which is not 0... so how does the script work (and it does work, thank you.)??? Link to comment https://forums.phpfreaks.com/topic/139743-solved-every-other-while-highlighting/#findComment-731132 Share on other sites More sharing options...
premiso Posted January 6, 2009 Share Posted January 6, 2009 Modulus does not just divide, it returns the remainder. 1/2 returns .5 as the remainder. 2/2 returns 0 as the remainder. 0/2 returns 0 as the remainder, 1 returns .5 as the remainder, rinse and repeat. Link to comment https://forums.phpfreaks.com/topic/139743-solved-every-other-while-highlighting/#findComment-731135 Share on other sites More sharing options...
Clinton Posted January 6, 2009 Author Share Posted January 6, 2009 Ahhhhhhhhhhhhh... gotcha, gotcha. Thank you. Link to comment https://forums.phpfreaks.com/topic/139743-solved-every-other-while-highlighting/#findComment-731136 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.