Format Alternate Rows in Different Colours
Posted: Thu Jul 31, 2008 3:37 pm
Hello folks
I'm attemping to format alternate rows in a table with different colours
I reckon I'm missing a "foreach" command and equally that line 27 "$row = mysql_fetch_assoc($data))" is probably wrong..
I just can't figure it out.
Can anybody kindly volunteer the correct syntax?
Thanks very much
Conor
I'm attemping to format alternate rows in a table with different colours
I reckon I'm missing a "foreach" command and equally that line 27 "$row = mysql_fetch_assoc($data))" is probably wrong..
I just can't figure it out.
Can anybody kindly volunteer the correct syntax?
Thanks very much
Conor
Code: Select all
<?php
include("../admin/config.php");
// fetch the project listings from the projects database
// count the number of data entry rows in the database
$data = mysql_query("SELECT projects.id, projects.name, projects.cat FROM projects ORDER BY projects.id ASC");
$rows = count($data);
// the two row colors to switch between
$rowcolor1 = '#F0F0F2';
$rowcolor2 = '#FFFFFF';
// the background colors on mouseover
$hovercolor1 = '#BAD4EB';
$hovercolor2 = '#DCE9F4';
echo '
<table style="caption-side: top;
border: 0.1em solid #eee;
border-collapse: collapse;
margin: 1em;
width: 30em;">
<caption style="font-weight: bold;">Demonstration of alternate row colors</caption>';
for($n = 0; $n < $rows; $n++)
$row = mysql_fetch_assoc($data))
{
// this is where the magic happens
if($n % 2 == 1)
{
// add more things to swop with each cycle
$style = $rowcolor1;
$hoverstyle = $hovercolor1;
}else{
$style = $rowcolor2;
$hoverstyle = $hovercolor2;
}
echo '
<tr id="row'.$n.'" style="background:'.$style.';"
onmouseover="this.style.background=\''.$hoverstyle.'\'"
onmouseout="this.style.background=\''.$style.'\'">
<td style="padding: 0.3em 1em;">'.$data[$n].'</td>
</tr>';
}
echo '
</table>';
?>