PHP Scripts

Alternate coloured rows in a table

This example reads a database, and displays results.
The example creates a table (you can view and format the table in standard html) and then pastes information, swapping row colours if the row result is divisible by 2..

<TABLE WIDTH="100%" HEIGHT="40" BORDER="0" CELLPADDING="10" CELLSPACING="5" BGCOLOR="#FFFFFF">
<TR bgcolor="#F0F0F0">

// General display the table information and titles of the columns

<TD HEIGHT="40" ALIGN=bottom ID="header"><em><FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="1" COLOR="#8A8A8A">Column A </FONT></em></TD>
<TD ALIGN=middle ID="header"><em><FONT COLOR="#8A8A8A" SIZE="1" face="Verdana, Arial, Helvetica, sans-serif">Column B</FONT></em></TD>
<TD ALIGN=middle ID="header"><em><FONT COLOR="#8A8A8A" SIZE="1" face="Verdana, Arial, Helvetica, sans-serif">Column C</FONT></em></TD>
<TD ALIGN=middle ID="header"><em><FONT COLOR="#8A8A8A" SIZE="1" face="Verdana, Arial, Helvetica, sans-serif">Column D </FONT></em></TD>
</TR>

//an empty spacer row
<TR>
<TD HEIGHT="40" ALIGN=middle ID="header">&nbsp;</TD>
<TD ALIGN=middle ID="header">&nbsp;</TD>
<TD ALIGN=middle ID="header">&nbsp;</TD>
<TD ALIGN=middle ID="header">&nbsp;</TD>
</TR>

<?php

$num=mysql_num_rows($result);
$i=0;

// Count rows in database and assign variables to results

while ($i < $num) {
$a=mysql_result($result,$i,"thumb");
$b=mysql_result($result,$i,"reference");
$c=mysql_result($result,$i,"more");
$d=mysql_result($result,$i,"price");

// alternate #8F8F8F with #000000

print ($i % 2) ? "<tr bgcolor=\"8F8F8F\">" : "<tr bgcolor=\"000000\">";

print "<td align=right>$a</td>";
print "<td align=right>$b</td>";
print "<td align=right>$c</td>";
print "<td align=right>$d</td>";

++$i;
}
?>


</table>

 

Back