2011-07-15 242 views
1

我試圖在排序,奇怪,甚至有不同的顏色更容易閱讀。表格tr背景顏色變化

這裏是我的代碼:

$show_res = mysql_query($show_query); 
while ($show_row = mysql_fetch_assoc($show_res)){ 
$rc++; 
if (($rc > 1)){ 
    $tr = '#cccccc'; 
} else { 
    $tr = '#ffffff'; 
}?> 
    <tr style="background-color:<?php echo $tr ;?>"> 

它不工作,我失去的東西嗎?

感謝您的幫助

+0

http://stackoverflow.com/questions/5958471/alternating-table-row-background-colors-on-a-dynamic-table – alexantd

+0

什麼瀏覽器是否在使用? – Jrod

+0

你還有別的東西會設置背景顏色嗎?像TD元素一樣?要麼? – Steven

回答

3

你想用模%

一些例子後:

  • 5%2 =餘數爲1
  • 4%2 =餘數爲0
  • 6%2 =餘數爲0
  • 9%2 =餘數爲1

因此根據餘數是1還是0來改變顏色。而且你希望<tr>元素成爲你的循環的一部分,因爲它不斷地從一種顏色或另一種顏色來回變化。

$show_res = mysql_query($show_query); 
while ($show_row = mysql_fetch_assoc($show_res)) 
{ 
    $rc++; 
    if ($rc % 2 == 1) 
    { 
     $tr = '#cccccc'; 
    } 
    else 
    { 
     $tr = '#ffffff';  
?> 
    <tr style="background-color:<?php echo $tr ;?>"> 
<? 
    } 
} 
?> 
+0

正是我在找的東西!謝謝 – Warface

+0

@Warface好吧,給我我的觀點,然後喲。我只幫助這個網站上的人獲得任意創建的點。 (綠色檢查): - D. – FinalForm

0

您是否在任何地方定義了$rc=0;

0

您遇到的問題似乎是您在增加$ rC++之後沒有重置它。嘗試將其設置爲0,然後重新設定你就遞增到1

1
$show_res = mysql_query($show_query); 
while ($show_row = mysql_fetch_assoc($show_res)){ 
$rc++; 
if (($rc > 1)){ 
    $tr = 'odd'; 
} else { 
    $tr = 'even'; 
}?> 
    <tr class="<?php echo $tr ;?>"> 

CSS

.odd td { background-color: #FFF; } 
.even td { backgorund-color: #F6F6F6; } 

使用CSS,並設置背景顏色爲TD不是TR元素。

0
$show_res = mysql_query($show_query); 
while ($show_row = mysql_fetch_assoc($show_res)) 
{ 
     if (isset($k) and $k==1) 
     { 
      echo '<tr class="EvenTableRows">'; 
      $k=0; 
     } else { 
      echo '<tr class="OddTableRows">'; 
      $k=1; 
     } 

     ..... more statement 

}