Jump to content

Php Mysql - How to alternate table row colors?


jalmz

Recommended Posts

Hi guys,

 

I would like to know on  How to alternate table row colors in Php Mysql result? CSS or Html?

 

Thanks

 

 <?  $result = mysql_query("SELECT * FROM tbl_sta WHERE fiesta_date > '$now' ORDER BY sta_date LIMIT 5");
  echo "<table id='table1' cellspacing='1' cellpadding='3'>
  <tr>
  <th>Upcoming Fiesta</th>
  </tr>";
  
  while($row = mysql_fetch_array($result)){
              
  $date = date("l, F j ",strtotime($row["sta_date"])); 

  echo "<tr>";
  echo "<td><a href='fiestasd.php' style='font-size:12px;font-weight:bold;'>" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "</a> <br />  $date  </td>";
  echo "</tr>";
  }
  echo "</table>"; ?>

 

Thank you.. more power phpfreaks!

	$i = 1;
$color1 = "#ff0000";
$color2 = "#00ff00";
while($row = mysql_fetch_array($result)){
	$date = date("l, F j ",strtotime($row["sta_date"])); 
	if($i%2 == 1) $color = $color1;
	else $color = $color2;
	echo "<tr style='background-color: $color'>";
	echo "<td><a href='fiestasd.php' style='font-size:12px;font-weight:bold;'>" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "</a> <br />  $date  </td>";
	echo "</tr>";
	$i++;
}

Very flashy colors, but you get the idea.

Same concept

	$i = 1;
while($row = mysql_fetch_array($result)){
	$date = date("l, F j ",strtotime($row["sta_date"])); 
	if($i%2 == 1) $trclass = "alt1";
	else $trclass = "alt2";
	echo "<tr class='$trclass'>";
	echo "<td><a href='fiestasd.php' style='font-size:12px;font-weight:bold;'>" . $row['fiesta_brgy'] . " " . $row['fiesta_town'] . "</a> <br />  $date  </td>";
	echo "</tr>";
	$i++;
}

There's no points system on this forum by the way.

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.