2011-07-15 156 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 

}