adam291086 Posted December 19, 2007 Share Posted December 19, 2007 I am trying to alternate the bgcolor of the echoed database query results. At the moment it is staying one color. I can't work out why. // Determine alternate row colours for ($i = 0; $i < $count; $i++){ if($i % 2 == 0){ $style = "bgcolor=\"#FFFFFF\""; $bottomstyle = "bgcolor=\"#FFFFFF\""; } else { $style = "bgcolor=\"#EFEFEF\""; $bottomstyle = "bgcolor=\"#EFEFEF\""; } } while($row = mysql_fetch_array($result)) { $id = $row['news_id']; $titlenews = $row['News_title']; $news = $row['News']; $caption = substr($news, 0, 50)."....."; ?> <table width="264" height="114" border="0"> <tr> <th <?php echo $style; ?> height="35" scope="row"><?php echo $titlenews;?></th> </tr> <tr> <th <?php echo $style; ?> height="79" scope="row"><?php echo '<a href="http://www.bcyorkshire.co.uk/news/shownews.php?news='.urlencode($id).'">'. $caption.'</a>'?></th> </tr> </table> <?php } include '../database/closedb.php'; ?> Thanks for any help Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 Do you want every other row a different color? If so, this works great if ($color == $color1){ $color = $color2; }ELSE{ $color = $color1; } Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 19, 2007 Author Share Posted December 19, 2007 That is just printing everything out white with the code <?php include '../database/config.php'; echo '<form action="modifynews.php" method="post">'; $result = mysql_query("SELECT * FROM `News` ORDER BY `news_id` DESC Limit 5") or die(mysql_error()); $count = mysql_num_rows($result); $color1 = "#ffffff"; $color2 = "#EFEFEF"; if ($color == $color1){ $color = $color2; }ELSE{ $color = $color1; } while($row = mysql_fetch_array($result)) { $id = $row['news_id']; $titlenews = $row['News_title']; $news = $row['News']; $caption = substr($news, 0, 50)."....."; ?> <table width="264" height="114" border="0"> <tr <?php echo $color; ?> > <th height="35" scope="row"> <?php echo $titlenews;?></th> </tr> <tr <?php echo $color; ?> > <th height="79" scope="row"><?php echo '<a href="http://www.bcyorkshire.co.uk/news/shownews.php?news='.urlencode($id).'">'. $caption.'</a>'?></th> </tr> </table> <?php } include '../database/closedb.php'; ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 You need to assign $color as well $color = "#ffffff"; $color1 = "#ffffff"; $color2 = "#99ffff"; Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 19, 2007 Author Share Posted December 19, 2007 still all one color ??? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 This works perfectly for me $color = "#ffffff"; $color1 = "#ffffff"; $color2 = "#99ffff"; while ($row = mysql_fetch_row($result)) { if ($color == $color1){ $color = $color2; }ELSE{ $color = $color1; } $id=$row[0]; echo "<tr bgcolor=$color>"; Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 19, 2007 Author Share Posted December 19, 2007 needed the if statment in the while loop. Thanks Quote Link to comment Share on other sites More sharing options...
richardw Posted December 19, 2007 Share Posted December 19, 2007 Here's one more simple one line statement while ($row = mysql_fetch_array($results)) { $bgcolor = ($bgcolor == "F1F5F8" ? "FFFFFF" : "F1F5F8"); Quote Link to comment Share on other sites More sharing options...
revraz Posted December 19, 2007 Share Posted December 19, 2007 That's nice, i may steal that. Quote Link to comment Share on other sites More sharing options...
richardw Posted December 19, 2007 Share Posted December 19, 2007 Here is one more example, not quite as simple but handles alternating colors in a checkerboard like grid: <?php $columns = 2; // define number of columns - use with a table or css grid // Connection string and query here $num_rows = mysql_num_rows($result); echo "<TABLE BORDER=\"1\" width=\"530\" cellpadding=\"3\" cellspacing=\"2\" bordercolor=\"#000066\">\n"; echo "<tr><td colspan=$columns bgcolor=\"#99CCFF\">"; echo "Put your Title contents here"; echo "</td></tr>"; // changed this to a for loop so we can use the number of rows $x = 0; for($i = 0; $i < $num_rows; $i++) { if ($x == 4) { $x = 1; } else { $x++ ; } if ($x == 1) { $bgcolor = "#F1F5F8"; } elseif ($x == 2) { $bgcolor = "#FFFFFF"; } elseif ($x == 3) { $bgcolor = "#FFFFFF"; } elseif ($x == 4) { $bgcolor = "#F1F5F8"; } else { $bgcolor = ($bgcolor == "#F1F5F8" ? "#FFFFFF" : "#F1F5F8"); } $row = mysql_fetch_array($result); if($i % $columns == 0) { //if there is no remainder, we want to start a new row echo "<TR>\n"; } echo "<TD valign=top bgcolor=$bgcolor width=50%>"; echo "Cell Contents Here"; echo "</TD>\n"; if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) { //if there is a remainder of 1, end the row echo "</TR>\n"; } } echo "</TABLE>\n"; } ?> I find that it helps in displaying results, especially if space is tight and a grid can meet the display requirements. Here is one example where I have used it: http://www.providenceri.com/BuyProvidence/buy_providence.php?category=all I hope this snippet can also be put to use Quote Link to comment 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.