Page 1 of 1

tr color variations help in PHP

Posted: Thu Dec 19, 2002 2:22 pm
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

Posted: Thu Dec 19, 2002 4:17 pm
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><?

Posted: Thu Dec 19, 2002 4:22 pm
by soshea
wow, looks cool! it tested great locally, so tonight i can test it on the server.
thanx for the help!
soshea

Posted: Thu Dec 19, 2002 8:19 pm
by soshea
worked great!! thanx
soshea