Posted By

Tamedo on 11/23/07


Tagged

table colour row


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

joeg
heinz1959
vali29
scyfox
benrasmusen
skywalker
nb109
mecha
adth


Alternating MYSQL table row colour


 / Published in: PHP
 

  1. <?php
  2. $host="localhost"; // Host name
  3. $username=""; // Mysql username
  4. $password=""; // Mysql password
  5. $db_name="test"; // Database name
  6. $tbl_name="test_mysql"; // Table name
  7.  
  8. // Connect to server and select databse
  9. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  10. mysql_select_db("$db_name")or die("cannot select DB");
  11.  
  12. $sql="SELECT * FROM $tbl_name";
  13. $result=mysql_query($sql);
  14.  
  15. // Define $color=1
  16. $color="1";
  17.  
  18. echo '<table width="400" border="1" align="center" cellpadding="2" cellspacing="0">';
  19. while($rows=mysql_fetch_array($result)){
  20.  
  21. // If $color==1 table row color = #FFC600
  22. if($color==1){
  23. echo "<tr bgcolor='#FFC600'>
  24. <td>".$rows['id']."</td><td>".$rows['name']."</td><td>".$rows['email']."</td>
  25. </tr>";
  26. // Set $color==2, for switching to other color
  27. $color="2";
  28. }
  29.  
  30. // When $color not equal 1, use this table row color
  31. else {
  32. echo "<tr bgcolor='#C6FF00'>
  33. <td>".$rows['id']."</td><td>".$rows['name']."</td><td>".$rows['email']."</td>
  34. </tr>";
  35. // Set $color back to 1
  36. $color="1";
  37. }
  38.  
  39. }
  40. echo '</table>';
  41. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: duartec2 on February 26, 2008

Keep it simple!

$color = '#FFC600'; while($rows=mysqlfetcharray($result)){ $color = $color == '#FFC600'? '#C6FF00':'#FFC600';

echo " ".$rows['id']."".$rows['name']."".$rows['email']." ";

}

You need to login to post a comment.