Jaap Van Nes
Jaap Van Nes

Reputation: 45

Change background color for different DIV's in PHP/SQL output

I'm trying to form my PHP/MySQL output in a 'Word-like' table format: one row is background color X, the next is bg color Y, then X again, then Y, etc.

To do this I've come up with a solution, but I'm guessing it's not really the neatest or cleanest way to do it. Check out my solution. I'm looking forward to your ideas/input!

$selectdoc = "SELECT * FROM document WHERE docid = '" . $_GET['docid'] . "' LIMIT 10";
$queryselectdoc = mysql_query($selectdoc);

$colorindicator = 1;

while($dbdoc = mysql_fetch_object($queryselectdoc)) {

   $title = "$dbdoc->title";

   $colorindicator = $colorindicator+1;

     if ( $i&1 )
      {
        echo "<div id=\"even\">My BG color is the one based on EVEN!</div>";
      }
     else
      {
        echo "<div id=\"odd\">My BG color is the one based on ODD!</div>";
      }

 }

Upvotes: 0

Views: 1294

Answers (2)

Bojangles
Bojangles

Reputation: 101543

You can do it using CSS pseudo classes :nth-child using a list or a table or something

Try using a list like this :-)

Upvotes: 1

hcb
hcb

Reputation: 8357

This is shorter:

echo ((++$colorindicator % 2)==1) ? "<div id=\"odd\">" : "<div id=\"even\">";
echo "contents</div>";

Upvotes: 0

Related Questions