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 ... Aliases from Mysql
echo "<table class='sortable' id='tablesorter' cellspacing='1' cellpadding='0' border='0' width='920px' >
<thead>
<tr>
<th class='header'>Short Name of Fund</th>
<th class='header'>I/C</th>
<th class='header'>Fund Manager Company</th>
<th class='header'>Class</th>
<th class='header'>Special Class</th>
<th class='header' id='custom'>TTR year-to-date<br /> %</th>
<th class='header'>Mgmt Fee Effectively Charged</th>
<th class='header id='custom'>Total Expenses <br /> %</th>
<th class='header'>Fund Size</th>
</thead><tbody>
</tr>";
//<tr> specifies table row. for each <td> (table data) will specify a new column. The $row specifies the mysql column name (in this case using an alias)
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><a href=\"page.php?id={$row['ID']}\">{$row['Short Name of Fund']}</a></td>";
echo "<td>" . $row['I/C'] . "</td>";
echo "<td>" . $row['Fund Manager Company'] . "</td>";
echo "<td>" . $row['Class'] . "</td>";
echo "<td>" . $row['Special Class'] . "</td>";
echo "<td>" . $row['TTR year-to-date %'] . "</td>";
echo "<td>" . $row['Mgmt Fee Effectively Charged'] . "</td>";
echo "<td>" . $row['Total Expenses %'] . "</td>";
echo "<td>" . $row['Fund Size'] . "</td>";
echo "</tr>";
}
echo "</tbody></table>";
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.
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
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:
<tr class="<?php echo $class ?>">
PS. sorry for my english:/ Hope it helped.