我想对结果表进行斑马条纹处理。我找不到一个可靠的解决方案。我需要对这个表做什么才能将奇数/偶数类添加到结果行?
// 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>";
这应该有效,语法可能不正确,因为我还没有测试过。但逻辑是存在的。
$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;
编辑:更新代码,使其正常工作。
如果您愿意使用CSS 3,请尝试CSS中的第n个子技巧:
tr:nth-child(2n+1)
{
background-color: #aaeeaa;
}
但是,如果您想继续使用奇数/偶数类,那么在while循环期间需要一个计数器,然后在count % 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。对不起我的英语:/希望有帮助。