View Full Version : CSS Styling A PHP Generated Table
tomyknoker
04-03-2007, 07:34 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>
</tr>
<?php
}
}
echo '</table>';
?>
It looks like you've only posted part of the code. At the end there are these things } which indicate that there was probably an if or a foreach before it . . . you need to post the rest of the code.
tomyknoker
04-03-2007, 08:16 AM
I thought that was all I needed.. Is this better?
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>
</tr>';
}
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);
//create the layout
?>
<link href="cs_style.css" rel="stylesheet" type="text/css" />
<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>
</tr>
<?php
}
}
echo '</table>';
?>
tomyknoker
04-03-2007, 08:53 AM
Ok I tried this but no luck... Can anyone take a look?
<?php
/* connect to the mysql database and use a query to get the members info */
include 'configure.php';
include 'open.php';
//navigation
include("nav.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>
</select>
<input type="submit" value="Search">
</form>
<?php
echo '<table width="700" border="0" cellspacing="0" cellpadding="0">
<caption>MEMBERS - BIRTHDAYS SEARCH Download Details</caption>
<thead>
<tr>
<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>
</thead>
<tr class="odd">
<td>On Hold </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>';
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>
</tr>';
}
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 % 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>
</tr>
<?php
$row_count++;
}
}
echo '</table>';
?>
boogyman
04-04-2007, 02:24 PM
<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>
</tr>
I am guessing that if you look at the source code you will see <td bgcolor=$row_color ...
this is because you have stopped the transmission of php. now you can either do <?php echo "" ?> for every single one of those or you could modify the script so you just parse it all in, and escape to php for your variable.
...
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 % 2) ? $color1 : $color2;
//create the layout
echo ' // Starts Output
<link href="cs_style.css" rel="stylesheet" type="text/css" />
<tr valign="top">
<td bgcolor="' . $row_color . '">' . $qry['FirstName'] . '</td>
<td bgcolor="' . $row_color . '">' . $qry['LastName'] . '</td>
<td bgcolor="' . $row_color . '">' . date('d/m/Y', strtotime($qry['DateOfBirth'])) . '</td>
<td bgcolor="' . $row_color . '">' . $qry['loginDateTime']?date('d/m/Y H:i:s', strtotime($qry['loginDateTime'])):'N/A') . '</td>
<td bgcolor="' . $row_color . '">' . $qry['State'] . '</td>
<td bgcolor="' . $row_color . '">' . $q['Name'] . '</td>
</tr>
'; // Ends Output
$row_count++;
}
}
echo '</table>';
?>
HTML supports both single and double quoted strings, so it is possible to do it this way, the other alternative, would be to backslash ( \ ) escape all of the double quotes that are intended to be html based.
It also might just be easier instead of coloring all the data cells, just to color the entire row ? then you wouldn't need the background color for all of the datacells.
echo ' // Starts Output
<link href="cs_style.css" rel="stylesheet" type="text/css" />
<tr valign="top" bgcolor="' . $row_color . '">
If you only have a couple of rows to do this on it won't be too much of a hassle, however if you have like 1000+ adding 1-2kb extra each ? thats alot of transfer that will take longer to download for the user?
...just a thought
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.