Log in

View Full Version : PHP TR Color Help



Morty222
02-13-2007, 09:39 PM
I am trying to alternate the TR color, can someone take a look and write something I can use to alternate the TR color. I have tried everything, no luck.

Here is my query:

$query = "select * from tl_tourny ORDER BY sdate ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){

$id = $row['id'];
$name = $row['name'];
$games = $row['games'];
$location = $row['location'];
$cost = $row['cost'];
$sdate = $row['sdate'];
$edate = $row['edate'];
$age = $row['age'];
$age2 = $row['age2'];
?>
<tr>
<td class="tournytextlite"><?php echo $sdate; ?></td>
<td class="tournytextlite"><?php echo $edate; ?></td>
<td class="tournytextlite"><?php echo $location; ?></td>
<td class="tournytextlite"><a href="details.php?id=<?php echo $id; ?>"><?php echo $name; ?></a></td>
<td class="tournytextlite"><?php echo $age; ?>&nbsp;&nbsp;<?php echo $age2; ?></td>
<td class="tournytextlite">$<?php echo $cost; ?></td>
<td class="tournytextlite"><?php echo $games; ?></td>
<td class="tournytextlite"><a href="tournament_reg<?php echo $age; ?>.php">Register</a></td>
</tr><?php } ?>

TheBigT
02-13-2007, 09:43 PM
1. Do you want a random color?
2. Where is your css file? If you want to create a different color, which will not change, you could easily do this: <tr class = "whatever"> and then in your css file put...

.whatever {
background: HEX/COLOR;
}

Morty222
02-13-2007, 09:45 PM
No, I actually need it to alternate TR colors each time the DB generates a new entry. Two different colors alternating TR's.

TheBigT
02-13-2007, 09:56 PM
$number = 0;
$query = "select * from tl_tourny ORDER BY sdate ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){

$id = $row['id'];
$name = $row['name'];
$games = $row['games'];
$location = $row['location'];
$cost = $row['cost'];
$sdate = $row['sdate'];
$edate = $row['edate'];
$age = $row['age'];
$age2 = $row['age2'];
$number++;
if($number & 1) $bgColor = 'color1';
else $bgColor = 'color2';

?>
<tr style = "background: <?php echo $bgColor; ?>;">
<td class="tournytextlite"><?php echo $sdate; ?></td>
<td class="tournytextlite"><?php echo $edate; ?></td>
<td class="tournytextlite"><?php echo $location; ?></td>
<td class="tournytextlite"><a href="details.php?id=<?php echo $id; ?>"><?php echo $name; ?></a></td>
<td class="tournytextlite"><?php echo $age; ?>&nbsp;&nbsp;<?php echo $age2; ?></td>
<td class="tournytextlite">$<?php echo $cost; ?></td>
<td class="tournytextlite"><?php echo $games; ?></td>
<td class="tournytextlite"><a href="tournament_reg<?php echo $age; ?>.php">Register</a></td>
</tr><?php } ?>

Just as a note, this is a cheap trick, I am not sure if there is an easier/more efficient way to do this.

Morty222
02-13-2007, 09:59 PM
Worked great, thanks I really appreciate it.

TheBigT
02-13-2007, 10:02 PM
Your welcome, it was no problem.