Jump to content

[SOLVED] alternating color depending on results returned


adam291086

Recommended Posts

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

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';
?>

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>";

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.