how do i get my table to alternate row colors using php?

Austin G

New member
<?php

include('connect.php');

mysql_select_db("gracom1_widgets", $con) or die(mysql_error());

$result = mysql_query("SELECT * FROM customers INNER JOIN states on states.stateID = customers.stateID WHERE custID = '$_GET[custID]'") or die(mysql_error());


echo "<table>
<tr>
<th>Customer Name</th>
<th>Phone</th>
<th>Email</th>
<th>Address</th>
<th>State</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['custName'] . "</td>";
echo "<td>" . $row['custPhone'] . "</td>";
echo "<td><a href='mailto:" . $row['custEmail'] . "'>" . $row['custEmail'] . "</a></td>";
echo "<td>" . $row['custAddress'] . "</td>";
echo "<td>" . $row['stateName'] . "</td>";

echo "</tr>";
}
echo "</table>";

?>
 
$i=0;
while($row = mysql_fetch_array($result))
{
if($i%2==0)
{
$col = ' style="background-color:red"';
}
else
{
$col = ' style="background-color:blue"';
}

echo "<tr ".$col.">";
echo "<td>" . $row['custName'] . "</td>";
echo "<td>" . $row['custPhone'] . "</td>";
echo "<td><a href='mailto:" . $row['custEmail'] . "'>" . $row['custEmail'] . "</a></td>";
echo "<td>" . $row['custAddress'] . "</td>";
echo "<td>" . $row['stateName'] . "</td>";

echo "</tr>";
}


the main concept is this...

ive not checked the coding thoroughly. please do so.

Hope this helps...........
 
Hi Austin

Why do you write code to generate HTML table? is not there any readymade component which can do this for you? I have done a lot of coding like this. Now I want to not spend my time genearting HTML output by hand. Can anybody respond on this.
 
Hi Austin

Why do you write code to generate HTML table? is not there any readymade component which can do this for you? I have done a lot of coding like this. Now I want to not spend my time genearting HTML output by hand. Can anybody respond on this.
 
$i=0;
while($row = mysql_fetch_array($result))
{
if($i%2==0)
{
$col = ' style="background-color:red"';
}
else
{
$col = ' style="background-color:blue"';
}

echo "<tr ".$col.">";
echo "<td>" . $row['custName'] . "</td>";
echo "<td>" . $row['custPhone'] . "</td>";
echo "<td><a href='mailto:" . $row['custEmail'] . "'>" . $row['custEmail'] . "</a></td>";
echo "<td>" . $row['custAddress'] . "</td>";
echo "<td>" . $row['stateName'] . "</td>";

echo "</tr>";
}


the main concept is this...

ive not checked the coding thoroughly. please do so.

Hope this helps...........
 
Back
Top