Page 1 of 1

CSS query results

Posted: Mon Feb 19, 2007 12:49 pm
by slash85
The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I'm doing a simple query and using a while to echo the results to a table and was wondering how i can set row1 to have say a red background and row2 to be blue.  I know how to do the CSS bit just not the php code.  In a nutshell i'd like all even number rows to be red all odds to be blue, any one know how i would code this?  Here's what i've got so far:

Code: Select all

<?php

while($row = mysql_fetch_array($result))
{

echo "<table border=1>".
"<tr>".
"<th>Horse</th>".
"<th>Course</th>".
"<th>Time</th>".
"<th>Last Backed</th>".
"</tr>".
"<tr>".
"<td>{$row['Name']}</td>".
"<td>{$row['Date']}</td>".
"<td>{$row['Time']}</td>".
"</tr>".
"<tr>".
"</table><br>";

} 

?>
Thanks for any info,
Slash.


The Ninja Space Goat | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Feb 19, 2007 12:52 pm
by Oren
We talked about this a lot... Use the search feature and search for "zebra".

Posted: Mon Feb 19, 2007 1:05 pm
by Kieran Huggins
You could use jQuery and leave that on the client side (I would):

Code: Select all

<script type="text/javascript" src="/path/to/jquery.js"></script>
<script type="text/javascript">
$(function(){
	$('table tr:even').css('color','red');
	$('table tr:odd').css('color','blue');
});
</script>
Or you could write classes to every odd row:

Code: Select all

echo "<table border=1>".
"<tr>".
"<th>Horse</th>".
"<th>Course</th>".
"<th>Time</th>".
"<th>Last Backed</th>".
"</tr>";

$odd=false;
while($row = mysql_fetch_array($result))
{
	$odd=!$odd; // toggle the oddness
	$class=$odd?'odd':'even'; // set the class text
	
	echo '<tr class="'.$class.'">'.
	"<td>{$row['Name']}</td>".
	"<td>{$row['Date']}</td>".
	"<td>{$row['Time']}</td>".
	"<td>{$row['LastBacked']}</td>".
	"</tr>";

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

Posted: Mon Feb 19, 2007 1:24 pm
by slash85
Thanks Huggins,

I think i'll give the php odd row idea a go. And if not at least i can have a search round for zebra tables. Would of neva thought of calling it that, i was searchin for stuff like CSS uery results and while CSS.

Thanks,
Slash.

Posted: Mon Feb 19, 2007 10:17 pm
by RobertGonzalez
Third post down in this forum - 'Useful Posts'.

Posted: Sat Feb 24, 2007 4:35 am
by Kieran Huggins
I could be going crazy (in fact I might be anyway), but I only see two posts in that thread ;-)

Posted: Sat Feb 24, 2007 8:21 am
by RobertGonzalez
Ok, that is the last time I point to the thread instead of the exact post. You're Kieran, there are only two posts in that thread and for the life of me I cannot find the post I was looking at when I posted that reply :oops: .

I will keep looking and post when I find it. I know it is here. I am just not sure where now.