multiple rows and alternate color

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Below I found a code to make multiple colums from the output of a DB, how
can I incorporate alternat colors to the multiple row snippet?

//set the number of columns
$columns = 2;

$query = "SELECT stuff FROM mystuff ORDER BY stuff";
$result = mysql_query($query);

//we add this line because we need to know the number of rows
$num_rows = mysql_num_rows($result);
echo "<TABLE BORDER=\"0\">\n";

//changed this to a for loop so we can use the number of rows
for($i = 0; $i < $num_rows; $i++) {
    $row = mysql_fetch_array($result);
    if($i % $columns == 0) {
        //if there is no remainder, we want to start a new row
        echo "<TR>\n";
    echo "<TD>" . $row['stuff'] . "</TD>\n";
    if(($i % $columns) == ($columns - 1) || ($i + 1) == $num_rows) {
        //if there is a remainder of 1, end the row
        //or if there is nothing left in our result set, end the row
        echo "</TR>\n";
echo "</TABLE>\n";

Re: multiple rows and alternate color

aznFETISH wrote:
Quoted text here. Click to load it

   This is a really tedious way of doing things.  Have a look at
HTML_Table package from PEAR.  Alternating row colors is just one line
into your code.  Something like this (given that $t is a HTML_Table):

$light = array('bgcolor' => '#CCCCCC');
$dark = array('bgcolor' => '#AAAAAA');


Site Timeline