Skip to content Skip to sidebar Skip to footer

Zebra Stripe Php Mysql Table

I would like to Zebra Stripe the results table. I can't find a solid solution. What would I need to do to this table to add a odd/even class to the results rows? // HTML ... Ali

Solution 1:

This should work, the syntax may be incorrect as I have not tested it. But the logic is there.

$currentState = 'odd';
$html = '';
while($row = mysql_fetch_array($result)){
    $currentState = ($currentState == 'odd' ? 'even' : 'odd');
    $html .= '<tr class="'.$currentState.'">';
    $html .= '<td><a href="page.php?id=' . $row['ID'] . '">'. $row['Short Name of Fund'] .'</a></td>';
    $html .= '<td>' . $row['I/C'] . '</td>';
    $html .= '<td>' . $row['Fund Manager Company'] . '</td>';
    $html .= '<td>' . $row['Class'] . '</td>';
    $html .= '<td>' . $row['Special Class'] . '</td>';
    $html .= '<td>' . $row['TTR year-to-date %'] . '</td>';
    $html .= '<td>' . $row['Mgmt Fee Effectively Charged'] . '</td>';
    $html .= '<td>' . $row['Total Expenses %'] . '</td>';
    $html .= '<td>' . $row['Fund Size'] . '</td>';
    $html .= '</tr>';
}
echo$html;

EDIT: Updated code so it works.

Solution 2:

If you are willing to use CSS 3, try the nth child trick in your css:

tr:nth-child(2n+1) 
{
  background-color: #aaeeaa;
}

However, if you want to go ahead with odd / even classes, then you'll need a counter during your while loop and then alternate the odd / even classes when count % 2 = 0

Solution 3:

the trick is to use php in a logic way:D The trick is to make a dynamic class for your table rows. So basicaly you'll have 2 classes one for odd and one for eaven. I'll give you an example below (dont mind mind my project):

$counter="";
                      while ($upit_row = mysql_fetch_array($upit_run)){
                            $korisnik_id = $upit_row['korisnik_id'];
                $tip_id = $upit_row['tip_id'];
                                $tip = mysql_fetch_array(mysql_query("SELECT naziv FROM  tip_korisnika WHERE tip_id = '$tip_id'"));
                            $kor_ime = $upit_row['korisnicko_ime'];
                            $ime = $upit_row['ime'];
                            $prezime = $upit_row['prezime'];
                            $email = $upit_row['email'];
                            $counter++;
                            $class="";
                        if ($counter%2){
                           $class="even";
                      } else{
                           $class="odd";
                      }

CSS looks like this:

tr.odd{
    background-color:white;     
}
tr.even{
    background-color:#FAFAFA;
}

The counter will count every row that php generates which creates a number which u you dived by 2 and get odd and even numbers. After that you use the if statment to define your class:D After that just insert the class variable in to the like this:

echo"<tr class='$class'>";

or like this:

<trclass="<?phpecho$class?>">

PS. sorry for my english:/ Hope it helped.

Post a Comment for "Zebra Stripe Php Mysql Table"