2012-09-13 111 views
1

我想斑馬條紋結果表。我找不到一個可靠的解決方案。我需要做些什麼來爲這個表添加一個奇數/偶數類到結果行?斑馬條紋PHP MYSQL表

// 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>"; 

回答

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; 

編輯:更新的代碼,所以它的工作原理。

+0

這就是我從這個語法錯誤,意外的'\'(T_NS_SEPARATOR),期待','或';' –

+0

我更正了使用/而不是\的語法。錯誤是警告:在 –

+0

中用零除法它適用於這個校正回聲「」; –

3

如果您願意使用CSS 3,儘量在你的CSS的nth child招:

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

不過,如果你想繼續與奇/偶類,那麼你就需要在你的while循環中有一個計數器,然後在奇數/偶數類中交替使用count % 2 = 0

+1

值得一提的是'tr:n-child(odd)'也適用。 – vonflow

+0

我希望。但我必須支持較舊的IE。 –

0

訣竅是以邏輯方式使用php:D技巧是爲表格行製作一個動態類。所以基本上,你將有2個類爲奇數和一個爲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的生成創造了許多其中U你潛入2並獲得單數和偶數。之後,你使用if statment來定義你的類:d 之後,只需插入類變量到這樣的:

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

或像這樣:

<tr class="<?php echo $class ?>"> 

PS。對不起,我的英語:/希望它有幫助。