justinh Posted January 9, 2009 Share Posted January 9, 2009 trying to alternate between row colors on my table. I came up with this, but It's not working =/ $rowcount = 1; $query = mysql_query("SELECT * FROM hrpartya"); echo "<TABLE width=\"70%\" bordercolordark=\"#000000\" bordercolorlight=\"#FFFFFF\" cellpadding=\"5\"><tr><td><center>Party Name:</center></td></tr>"; while($showparty = mysql_fetch_array($query)){ if($rowcount = 1){ echo "<tr bgcolor=\"#DDD2C3\"><td><p align=\"center\">".$showparty['party_name']."</p></td></tr>"; } if($rowcount = 2){ echo "<tr bgcolor=\"#A49A8C\"<td><p align=\"center\">".$showparty['party_name']."</p></td></tr>"; } ++$rowcount; if($rowcount = 3){ $rowcount = 1; } } Seems logical to me.. thanks for any help. Link to comment https://forums.phpfreaks.com/topic/140192-row-color-alternation/ Share on other sites More sharing options...
justinh Posted January 9, 2009 Author Share Posted January 9, 2009 fixed it. should be == not = Link to comment https://forums.phpfreaks.com/topic/140192-row-color-alternation/#findComment-733604 Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 $rowcount = 0; $query = mysql_query("SELECT * FROM hrpartya"); echo "<TABLE width=\"70%\" bordercolordark=\"#000000\" bordercolorlight=\"#FFFFFF\" cellpadding=\"5\"><tr><td><center>Party Name:</center></td></tr>"; while($showparty = mysql_fetch_array($query)){ if (($rowcount%2) == 0) { echo "<tr bgcolor=\"#DDD2C3\"><td><p align=\"center\">".$showparty['party_name']."</p></td></tr>"; }else { echo "<tr bgcolor=\"#A49A8C\"<td><p align=\"center\">".$showparty['party_name']."</p></td></tr>"; } $rowcount++; } Modulus is what you want (the % operator). Link to comment https://forums.phpfreaks.com/topic/140192-row-color-alternation/#findComment-733605 Share on other sites More sharing options...
rhodesa Posted January 9, 2009 Share Posted January 9, 2009 all your = should be ==, but this way is more common: <?php $rowcount = 0; $query = mysql_query("SELECT * FROM hrpartya"); echo "<TABLE width=\"70%\" bordercolordark=\"#000000\" bordercolorlight=\"#FFFFFF\" cellpadding=\"5\"><tr><td><center>Party Name:</center></td></tr>"; while($showparty = mysql_fetch_array($query)){ $color = ($rowcount%2) ? '#DDD2C3' : '#A49A8C'; echo "<tr bgcolor=\"{$color}\"><td><p align=\"center\">".$showparty['party_name']."</p></td></tr>"; $rowcount++; } ?> edit: premiso...you are just too damn fast...i posted mine again though cus it's cleaner IMO Link to comment https://forums.phpfreaks.com/topic/140192-row-color-alternation/#findComment-733607 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.