Forum Moderators: coopster

Message Too Old, No Replies

Alternating row colors

row colors alternating

         

Awful newbie

8:11 pm on Apr 8, 2007 (gmt 0)

10+ Year Member



I wonder how to make the output from rows differ in two different colors? One background color and one alternating colro to improve readability?

Here is the output code:

$num_rows = mysql_num_rows($permit);
if ($num_rows == 0){
echo "<p>not allowed</p>";
echo "back ti <a href='index.php'>start<a/>";}
else{

if (isset($r_showcat)){

$showdoc = mysql_query("SELECT * FROM $prefix"."documents where catid=$r_doccat ");
while ( $rowshowdoc = mysql_fetch_array($showdoc) ) {
echo "<table width=100%><tr><td>";
echo("<b><span class=mini>Doc:</b> ".$rowshowdoc ["name"]."</td><td class=mini align='left'>Date: " . $rowshowdoc ["date"] . "</td><td><b><a target='_blanc' href='./docs/".$rowshowdoc ["name"]."'>show doc</a></b></td></tr>\n");
echo "<tr><td colspan='3'><i class=mini>".$rowshowdoc ["description"]."</i></td></tr>";
echo "</tr></table><br> \n";
} }

eelixduppy

8:44 pm on Apr 8, 2007 (gmt 0)



You could use the modulus operator [us2.php.net]. Here is a thread to get you started: [webmasterworld.com...]

You may also want to give a search [google.com] a try to see what comes up.

Good luck! :)

sawatkins

5:19 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Instead of the modulus, I like to use a simple if/then or switch statement. I remember my 10th grade programming teacher instructing us on this:

At the beginning of your operation, set a variable like $togglecolor as 0. Use it to switch back and forth from each line.

$togglecolor == 0;

Then, inside your for or while loop, where you are defining your colors:

if ($togglecolor == 0) {
// color it one way
$togglecolor = 1;
} else {
// the other color
$togglecolor = 0;
}

Hope this helps.

</g>

bysonary

7:38 pm on Apr 10, 2007 (gmt 0)

10+ Year Member



Someone on this forum told me exactly how to do this and it works like a charm, heres how i/that person does it.

Create 2 CSS classes called bg_0 and bg_1


.bg_1
{
background-color: #E3E3E3;
}

.bg_0
{
background-color: #FFFFFF;
}

Then in your table do this, include this in a while loop like so


<?php
while ($var = mysql_fetch_array(...))
{
$i = ($i + 1) % 2;

echo "<table border=\"0\" width=\"100%\" class=\"bg_$i\">";
echo "<tr>";
echo "<td>Hello</td>";
echo "</tr>";
}
?>

Remember too that if you are using this is multiple tables to reset the value of $i to 0 just do something like

$i = 0

you get the idea? my HTML etc might be a bit sloppy and stuff but im sure you can work it out so it works with your code.

HTH

Chris