1

我想 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>";
4

3 回答 3

4

这应该可行,语法可能不正确,因为我没有测试过。但逻辑是存在的。

$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;

编辑:更新代码,使其工作。

于 2012-09-13T16:16:49.697 回答
3

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

于 2012-09-13T16:16:10.513 回答
0

诀窍是以逻辑方式使用 php:D 诀窍是为您的表行创建一个动态类。所以基本上你会有两个班级,一个是奇数,一个是偶数。我会在下面给你一个例子(不要介意我的项目):

                            $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。对不起我的英语:/希望它有所帮助。

于 2014-05-03T16:30:17.940 回答