tr color variations help in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
soshea
Forum Commoner
Posts: 31
Joined: Tue Nov 12, 2002 11:22 pm
Location: Idaho
Contact:

tr color variations help in PHP

Post by soshea »

i am working making the colors alternate on each row of output from a query, but have not nailed it. i know comming from a coldfusion (ouch) background that it would be iif mod. any ideas with php? (ignore the db connection information i am working off of memory and i need a bit more programming to get it down)

Code: Select all

<?php
$dbi = (database = yadayada, user=postgres);
$sql = $pg_connect($dbi, "select * from testtable"); 
$rows = $pg_fetchrow($sql);
//here is the area ignore the above, doing it off memory
for ($i=0;$i<$rows;$i++) {
echo "<table><tr><td> test output </td></tr>";
// this is where the alternating colors should be
echo "<tr bgcolor = #DDDDDD>
// rest of code here
?>
any ideas on where to start??!?
thanx for any sugestions!
soshea
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

here's an example of something I used like that. I declared 2 color variables that hold the color string i want to use for the alternating rows.

$color_var = "color".($i % 2 + 1) builds a string that when $i is odd will evaluate to "color2" and when even "color1" the % operator gets the remainder from integer division.

${$color_var} builds the variable name to use for the rows color.

Code: Select all

<?php 

$color1 = "#c0c0c0";
$color2 = "#9690cc";
?><table><?
for($i = 0; $i < 10; $i++) {
    $color_var = "color". ($i % 2 + 1);
    ?><tr bgcolor="<? echo ${$color_var} ?>"><td>testing</td></tr><?
}
?></table><?
User avatar
soshea
Forum Commoner
Posts: 31
Joined: Tue Nov 12, 2002 11:22 pm
Location: Idaho
Contact:

Post by soshea »

wow, looks cool! it tested great locally, so tonight i can test it on the server.
thanx for the help!
soshea
User avatar
soshea
Forum Commoner
Posts: 31
Joined: Tue Nov 12, 2002 11:22 pm
Location: Idaho
Contact:

Post by soshea »

worked great!! thanx
soshea
Post Reply