PDA

View Full Version : Row Color



Titan85
01-12-2007, 09:05 PM
I just finished up a script that displays all users in a database and noticed that it is hard to read because every row looks the same. So I was hoping that there is a way to have the scrip assign a color (through a css class) to each row so that it can alternate colors. Here is how I am echoing the the lines of users:
echo'
<tr>
<td>'.$username.'</td>
<td>'.$email.'</td>
<td>'.$signup.'</td>
<td>'.$ip.'</td>
<td><a href="index.php?act=chk_delete&id='.$id.'">[X]</a></td>
<td><a href="index.php?act=edit&id='.$id.'">Edit</a></td>
<td><a href="index.php?act=chk_ban&id='.$id.'">Ban</a></td>
</tr>';
}Thanks for any help :)

alexjewell
01-12-2007, 09:40 PM
Well, what I think you want to know is this: http://alistapart.com/articles/zebratables

I am kind of confused about how you're echoing them, though - does $username, for example, echo ALL the usernames at once?

Titan85
01-12-2007, 10:26 PM
That is what I want to do, but have the php do it automatically. I forgot a part of the code, here is the full thing:
while($row = mysql_fetch_array($result)) {
extract($row);

echo'
<tr>
<td>'.$username.'</td>
<td>'.$email.'</td>
<td>'.$signup.'</td>
<td>'.$ip.'</td>
<td><a href="index.php?act=chk_delete&id='.$id.'">[X]</a></td>
<td><a href="index.php?act=edit&id='.$id.'">Edit</a></td>
<td><a href="index.php?act=chk_ban&id='.$id.'">Ban</a></td>
</tr>';
}

echo 'The while(mysql_fetch_array($result) { section tells it to do whatever is specified as long as there is another result. Thanks for the help so far

Titan85
01-13-2007, 06:06 AM
Anyone know how to have php automatically assign a class to each row?

thetestingsite
01-13-2007, 06:40 AM
Are you talking about alternating row colors for each row (similar to the one I have here (http://www.thetestingsite.net/forum/?page=mlist))? If so, the code is actually pretty simple. Don't have time to post now, but will do it first thing tomorrow morning.

thetestingsite
01-13-2007, 03:20 PM
As promised, this is the code I used to make the rows alternate colors (I took the liberty of integrating it with the code you posted above):




$count = 1;

$color = "#EEEEEE";
while($row = mysql_fetch_array($result)) {
extract($row);

echo'
<tr style="background-color: '.$color.'">
<td>'.$username.'</td>
<td>'.$email.'</td>
<td>'.$signup.'</td>
<td>'.$ip.'</td>
<td><a href="index.php?act=chk_delete&id='.$id.'">[X]</a></td>
<td><a href="index.php?act=edit&id='.$id.'">Edit</a></td>
<td><a href="index.php?act=chk_ban&id='.$id.'">Ban</a></td>
</tr>';
}

++$count;
if ($color == "#EEEEEE") {
$color = "#CCCCCC";
} else {
$color = "#EEEEEE";
}
}



Hope this helps.

Titan85
01-13-2007, 03:37 PM
That is exactly what I want to do, but when I tried it, it assigned the same color to all the rows. Did you mistype something maybe, or is it something I messed up? I don't understand this part:
if ($color == "#EEEEEE") {
$color = "#CCCCCC";
} else {
$color = "#EEEEEE";
} Isn't the first part saying that if $color = #EEEEEE, make it #CCCCCC?

thetestingsite
01-13-2007, 04:55 PM
I think I know what's wrong with it. In the code I posted above, I added an extra bracket (probably from copying and pasting from your posted code). Here's how it should look:




$count = 1;

$color = "#EEEEEE";
while($row = mysql_fetch_array($result)) {
extract($row);

echo'
<tr style="background-color: '.$color.'">
<td>'.$username.'</td>
<td>'.$email.'</td>
<td>'.$signup.'</td>
<td>'.$ip.'</td>
<td><a href="index.php?act=chk_delete&id='.$id.'">[X]</a></td>
<td><a href="index.php?act=edit&id='.$id.'">Edit</a></td>
<td><a href="index.php?act=chk_ban&id='.$id.'">Ban</a></td>
</tr>';


++$count;
if ($color == "#EEEEEE") {
$color = "#CCCCCC";
} else {
$color = "#EEEEEE";
}
}




Isn't the first part saying that if $color = #EEEEEE, make it #CCCCCC?

Yes, you are correct. This is what changes the colors of the cells.

Sorry I can't further explain it at this time, but perhaps someone else could.

Hope this helps.

Titan85
01-14-2007, 12:21 AM
I found the problem, it was that I put the code outside of my while statement. So it was my fault ;)

thetestingsite
01-14-2007, 01:38 AM
Glad you found the problem and got it resolved. Also, glad it is working for you. Let me know if you need any more help.

NXArmada
01-18-2007, 06:55 PM
Heres a quick and simple way.

CSS:


tr#even { background-color:#C3D9FF; }


PHP:


<?php
$tablerow_count=0;
function tablerowswitch() {
global $tablerow_count;
$tablerow_count++;
if ($tablerow_count % 2) {
echo "odd";
}
else {
echo "even";
}
}
?>


And set the id to this:


id="tablerowswitch()"

thetestingsite
01-19-2007, 03:27 AM
And set the id to this:


id="tablerowswitch()"



That's assuming you are still in the PHP code. If you "escape" your HTML code from your PHP code, you would need to reopen the php tags.

example:



<?php
if ($something) {
?>

<div id="<?php tablerowswitch(); ?>">

<?php
}
?>


Otherwise, it would just display the text tablerowswitch() in the id of the div/table.

Hope this helps.

NXArmada
01-22-2007, 01:53 PM
Yea well at least you get the idea lol. I should have put that in there for the new users to the PHP section.