View Full Version : CSS Styling A PHP Generated Table

04-03-2007, 09:33 AM
I have a layout on my table cells which I guess some people call a zebra layout. All the odd rows are say blue and all the even are blue. So say the first row blue, second red and so on... It's easy to do this in a simple table, but for my php page which shows the results do they all have to be odd? Of anyone can tell me a simple way of doing this...

<tr valign="top" class="odd">
<td><?php echo $qry['FirstName']; ?></td>
<td><?php echo $qry['LastName']; ?></td>
<td><?php echo date('d/m/Y', strtotime($qry['DateOfBirth'])); ?></td>
<td><?php echo($qry['loginDateTime']?date('d/m/Y H:i:s', strtotime($qry['loginDateTime'])):'N/A'); ?></td>
<td><?php echo $qry['State'];?></td>
<td><?php echo $q['Name'];?></td>

echo '</table>';

04-03-2007, 11:06 AM

04-03-2007, 11:10 AM
Ok I tried this but no luck... Can anyone take a look?


/* connect to the mysql database and use a query to get the members info */

include 'configure.php';
include 'open.php';


//assign the bday variable.
$month = $_REQUEST['month'];

$color1 = "#CCFFCC";
$color2 = "#BFD8BC";
$row_count = 0;

//birthday search
$info = mysql_query("SELECT * FROM `tblmembers` WHERE (MONTH(DateOfBirth) = '{$month}') AND `MemberApproved`='A'");

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
Brithday Month: <select name="month">
<option>Select A Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
<input type="submit" value="Search">

echo '<table width="700" border="0" cellspacing="0" cellpadding="0">
<caption>MEMBERS - BIRTHDAYS SEARCH Download Details</caption>
<th scope="col" class="left">FIRST NAME</th>
<th scope="col">LAST NAME</th>
<th scope="col">DATE OF BIRTH</th>
<th scope="col">LAST LOGIN</th>
<th scope="col" class="right">STATE</th>
<tr class="odd">
<td>On Hold </td>

if (mysql_num_rows($info) < 1) {
echo '<tr valign="top">
<td colspan="5>There are no members that match the query. Please go back and try again</td>

else {
while ($qry = mysql_fetch_array($info)) {

$rep = $qry['rep_NBR'];

$repInfo = mysql_query("SELECT * FROM `tblrepresentatives` WHERE `rep_NBR`='$rep'");
$q = mysql_fetch_array($repInfo);
$row_color = ($row_count &#37; 2) ? $color1 : $color2;

//create the layout
<link href="cs_style.css" rel="stylesheet" type="text/css" />

<tr valign="top">
<td bgcolor="$row_color" ><?php echo $qry['FirstName']; ?></td>
<td bgcolor="$row_color"><?php echo $qry['LastName']; ?></td>
<td bgcolor="$row_color"><?php echo date('d/m/Y', strtotime($qry['DateOfBirth'])); ?></td>
<td bgcolor="$row_color"><?php echo($qry['loginDateTime']?date('d/m/Y H:i:s', strtotime($qry['loginDateTime'])):'N/A'); ?></td>
<td bgcolor="$row_color"><?php echo $qry['State'];?></td>
<td bgcolor="$row_color"><?php echo $q['Name'];?></td>



echo '</table>';

04-03-2007, 11:16 AM
&#37; 2)
should be % 2

04-03-2007, 03:44 PM
You are also better off putting this into CSS (the color).

$rowColor = $rowCount % 2 ? "odd" : "even";
echo '<tr class="' . $rowColor . '">';

tr.odd {
background-color : #CCFFCC;

tr.even {
background-color : #BFD8BC;

Remember, PHP produces your HTML, HTML contains your information and CSS handles what it looks like.

04-03-2007, 04:10 PM
I always found a simpler method so you dont have to use count

$class = $class == "odd" ? "even" : "odd";

04-03-2007, 04:13 PM
But you are repeating a string!

Just kidding ;)

There's many ways of doing it. I was mostly using what he had already in there.