NameSilo

Alternating row colours

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Have you ever wanted to make a table with alternating row colours?

Well I did it for a site and thought it would be great to share it. Here

PHP:
<?php
//connect to db and select table
$db1 = mysql_connect("host", "user", "pass");
mysql_select_db("$db",$db1);
$table = "tablename"; //table name


$list = mysql_query("SELECT * FROM `$table` ORDER BY `coulmn_name` ASC "); // the SQL of the table you will show
echo"<h3><p align=center> $table <p></h3>"; //title for the page
echo('<table width="500px" height="110px" style="font-family: Verdana; border: 1px solid black;" align="center" cellpadding="2" cellspacing="1" border="0">'); //add the table
echo('<tr align="center"><td><b><u>Date</u></b></td><td><b><u>description</u></b></td></tr>'); // top row with the titles
while($get=mysql_fetch_array($list))
{
//chooses colous
if ($color == "CCFFCC")
{
$color = "FFFFFF";//change the code
}
else
{
$color = "CCFFCC"; //change the code
}

echo('<tr bgcolor=#'.$color.' align="center">  <td>'. $get['Date'] .'</td><td>'. $get['description'] .'</td></tr>');  //add the contents
}
echo("</table>"); //finish the table

?>

See it on action at: http://www.asgsoft.net/mosque/view all of a month.php
 
1
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Thank you!!!! This is exactly what I've been looking for lately...I'll try this out as soon as I can get my monitor working :(

But thank you so much!
 
0
•••
Nice Tutorial ... another way would be:

PHP:
echo "<table>";
$color1 = "#EEEEEE"; 
$color2 = "#FFFFFF"; 
$rowc = 0; 

$sql = mysql_query("SELECT * FROM places ORDER BY id LIMIT 5");

while ($row = mysql_fetch_array($sql) { 

$row_color = ($rowc % 2) ? $color1 : $color2;  //basicly like a if()

echo "
   <tr>
    <td width="110" bgcolor="$row_color" nowrap>
    $article_date</td>
    <td bgcolor="$row_color">
    <a href="whteva.php?cmd=full_article&article_id=$article_id">$article_title</a></td>
    </tr>";
   
   

    $rowc++; //increment by 1
} 
echo"</table>";
 
0
•••
I used something similar to this on my site (in my signature) it works really well. Tables can be hard to read, but alternating colors adds just enough of a difference that you can navigate through them.

rep points added to you guys!
 
0
•••
wackyjoe said:
Nice Tutorial ... another way would be:

PHP:
echo "<table>";
$color1 = "#EEEEEE"; 
$color2 = "#FFFFFF"; 
$rowc = 0; 

$sql = mysql_query("SELECT * FROM places ORDER BY id LIMIT 5");

while ($row = mysql_fetch_array($sql) { 

$row_color = ($rowc % 2) ? $color1 : $color2;  //basicly like a if()

echo "
   <tr>
    <td width="110" bgcolor="$row_color" nowrap>
    $article_date</td>
    <td bgcolor="$row_color">
    <a href="whteva.php?cmd=full_article&article_id=$article_id">$article_title</a></td>
    </tr>";
   
   

    $rowc++; //increment by 1
} 
echo"</table>";

Yes, this is the best way I found. I've been using this way ever since :)
 
0
•••
The second way is usually the method that I use, just much simpler putting the if on one line.
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomainEasy — Payment Flexibility
DomDB
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back