PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
cupaball
Forum Commoner
Posts: 85 Joined: Sun Feb 12, 2006 1:46 pm
Post
by cupaball » Sat Jun 10, 2006 5:10 pm
Can someone tell me how to make this code display alternate row colors and add a table border?
Code: Select all
<?
$username="";
$password="";
$database="";
mysql_connect("",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SELECT * FROM contact");
echo "<table>";
echo "<tr> <th>ID No.</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Phone Number</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['Email'];
echo "</td><td>";
echo $row['Phone'];
echo "</td><td>";
echo $row['Address'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['State'];
echo "</td><td>";
echo $row['Zip'];
echo "</td><tr>";
}
echo "</table>";
?>
derchris
Forum Commoner
Posts: 44 Joined: Sat Jun 10, 2006 6:14 pm
Post
by derchris » Sat Jun 10, 2006 6:29 pm
Here is how I would do it.
I also changed your code a little.
Code: Select all
<?
$color1 = "#CCFFCC";
$color2 = "#BFD8BC";
$row_count = 0;
$username="";
$password="";
$database="";
mysql_connect("",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query("SELECT * FROM contact");
echo "<table border='1'>
<tr> <th>ID No.</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>Phone Number</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
</tr>";
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$first = $row['FirstName'];
$last = $row['LastName'];
$email = $row['Email'];
$phone = $row['Phone'];
$address = $row['Address'];
$city = $row['City'];
$state = $row['State'];
$zip = $row['Zip'];
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<tr>
<td bgcolor='$row_color'>
$id</td>
<td bgcolor='$row_color'>
$first</td>
<td bgcolor='$row_color'>
$last</td>
<td bgcolor='$row_color'>
$email</td>
<td bgcolor='$row_color'>
$phone</td>
<td bgcolor='$row_color'>
$address</td>
<td bgcolor='$row_color'>
$city</td>
<td bgcolor='$row_color'>
$state</td>
<td bgcolor='$row_color'>
$zip</td><tr>";
$row_count++;
}
echo "</table>";
?>
cupaball
Forum Commoner
Posts: 85 Joined: Sun Feb 12, 2006 1:46 pm
Post
by cupaball » Wed Jun 14, 2006 9:46 am
thanks, I will give it a try and let you know.