我想Zebra Stripe的结果表。我找不到可靠的解决方案。我需要对此表做什么才能在结果行中添加奇数/偶数类?
// 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>";
答案 0 :(得分:3)
如果您愿意使用CSS 3,请尝试使用CSS中的nth child技巧:
tr:nth-child(2n+1)
{
background-color: #aaeeaa;
}
但是,如果你想继续使用奇数/偶数类,那么你在while循环中需要一个计数器,然后在count % 2 = 0
答案 1 :(得分:3)
这应该有效,语法可能不正确,因为我还没有测试过。但逻辑是存在的。
$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;
编辑:更新了代码,使其正常运行。
答案 2 :(得分:0)
诀窍是以逻辑方式使用php:D技巧是为表行创建一个动态类。所以基本上你将有两个类一个用于奇数,一个用于eaven。我将在下面给你一个例子(不要介意我的项目):
$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看起来像这样:
tr.odd{
background-color:white;
}
tr.even{
background-color:#FAFAFA;
}
计数器会计算php生成的每一行,它会创建一个你潜入2并得到奇数和偶数的数字。之后,您使用if语句来定义您的类:D 之后只需将类变量插入如下:
echo "<tr class='$class'>";
或者像这样:
<tr class="<?php echo $class ?>">
PS。对不起我的英语:/希望它有所帮助。