Forum Moderators: coopster

Message Too Old, No Replies

PHP forum-y-type-guestbook-thingy listings

How do I change colours on the list?

         

Richie Graham

6:49 pm on May 4, 2003 (gmt 0)

10+ Year Member


OK... I've got a guestbook-type-forum on my site which I've adapted from a code found here. I've set it up in a table so each table row contains a different message, and was wondering how I'd be able to set the background colour of the table row so it alternated, say between two shades of yellow or something. The PHP code I've got for the site is below.

TIA

Richie

---

<table border="1" style="border-collapse: collapse" bordercolor="#000000" width ="500"><tr><td width="121" valign="top">

<a name="requests">
<?php

if (!$name) {$name = "Anonymous";}
if ($message)
{
$name = strip_tags($name);
$email = strip_tags($email);
$homepageurl = strip_tags($homepageurl);
$homepagename = strip_tags($homepagename);
$message = strip_tags($message);
$message = ereg_replace("\r\n\r\n", "\n<P>", $message);

if (get_magic_quotes_gpc())
{
$message = stripslashes($message);
$name = stripslashes($name);
$from = stripslashes($from);
}

$date = date("D, M j Y");
$time = date("g:ia T");
$message = "<font size=\"2\"><font size=\"2\"><B>$name </B> <br>$date<br>$time<br><a href=mailto:$email>$email</a></td><td width=\"376\">$message</tr></td><tr><td width = \"121\" valign=\"top\">\r\n";

$textfile = "requests.txt";
if ($fp = @fopen ($textfile, "r"))
{
$oldmessages = @fread($fp,filesize($textfile));
@fclose(fp);
}

$fp = fopen ($textfile, "w");
fwrite ($fp, $message);
fwrite ($fp, $oldmessages);
fclose ($fp);
}
@readfile("requests.txt");
?>
</table>

jatar_k

6:55 pm on May 4, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Richie_Graham,

I usually use a flag to do it

$color1 = "336699";
$color2 = "666666";
$cflag = 1;

if ($cflag == 1) {
echo $color1;
$cflag = 2;
} else {
echo $color2;
$cflag = 1;
}

loop through that and it will alternate. That is a very simplified version since it is less code to use variable variables but that should give you an idea.

dmorison

6:55 pm on May 4, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Richie,

Here's generic code for alternating anything. You should be able to map this into your rendering code...

Start a counter at 1:

$i = 1;

On each itteration of your loop

if ($i++ % 2)
{
// do something
}
else
{
// do something else
}

This works because $i % 2 (the remainder of $i divided by 2) is true only when $i is odd.

QED.