Hiya I have a dynamic table and am trying to get the rows to be two different alternate colours. well Ive looked at a couple of snippets of this colour code and previous mails regarding this. Im having major troubles intergrating any of these suggestions with my code. Can anyone suggest where I am going wrong. Main problems areWithin the loop that prints the rows, whcih is usually a while loop. But that depends on your preferences.
1. where to put the loop for changing the colour (complicated due to the loop retrieving data from db)i.e in the do loop....in the while loop?
I see that you are using a helping variable $colorset.2. Also how to echo my rows in the colours.(something like this I think) print "<tr bgcolor='$trcolor'>"$db_fetch['bugid']; ?></td>;
e.g here is the code snippet for alternate coloured rows $trcolor="#F0F8FF"; while ($myrow = mysql_fetch_array($result)){ $colorset=0; if ($trcolor=='#F0F8FF'){ $trcolor='#B0C4DE'; $colorset=1; } if ($colorset==0){ if ($trcolor=='#B0C4DE'){ $trcolor='#F0F8FF';} } print "<tr bgcolor='$trcolor'>"; }
I have a problem reading your code in email, as i do not see hte indents well, so i restructure it here with _ underscores to make the indents survive the email program.
$trcolor="#F0F8FF"; //color#1
while ($myrow = mysql_fetch_array($result)) { ___$colorset=0; ___if ($trcolor=='#F0F8FF') //color#1 ___{$trcolor='#B0C4DE'; //color#2 ____$colorset=1; ___}
___if ($colorset==0) ___{ if ($trcolor=='#B0C4DE') //color#2 ______{$trcolor='#F0F8FF'; //color#1 ______} ___}
print "<tr bgcolor='$trcolor'>"; }
Lets walk through.
In the 1st walk,
1a) you enter with $trcolor=#1 and colorset=0.
2b) the first 'if' then sets it to $trcolor=#2 and colorset=1
3c) The second if sees that both conditions are true and set the color back to $trcolor=#1.
So the first row prints color1.
Ok. The code remembers the values, which are color#1 and colorset1.
In the next walkthrough,
2a) the colorset is set to 0 to start with.
At this moment you have the exact situation as with 1a). So the rest of the code will lead inevitably to the same color.
Do you see that?
It would be much easier to see what is happening if you would have only colorset toggling its value and just before printing, decide what the color is as a result of the value of colorset. You are doing trest on the color as well as the $colorset which is making the code very sensitive to errors, as well as difficult to understand.
Give it a try!
Basically: (pseudocode)
$colorset=0;
while (....) { toggle $colorset (toggle: if 1 then 0 and opposite) if ($colorset==0) $trcolor="#ffffff"; else $trcolor="#aaaaa"; print $trcolor. }
Chris Hayes
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php