multiple rows and alternate color

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aznFETISH

    multiple rows and alternate color

    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?

    <?php
    //set the number of columns
    $columns = 2;

    mysql_connect(' localhost','',' ');
    mysql_select_db ('test');
    $query = "SELECT stuff FROM mystuff ORDER BY stuff";
    $result = mysql_query($qu ery);

    //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_arr ay($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";
    ?>


  • Marcin Dobrucki

    #2
    Re: multiple rows and alternate color

    aznFETISH wrote:[color=blue]
    > 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?
    >
    > <?php
    > //set the number of columns
    > $columns = 2;[/color]
    ....

    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');
    $t->altRowAttribut es(1,$light,$da rk,TRUE);
    ....

    /Marcin

    Comment

    Working...