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!

Link to comment
Share on other sites

	$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.