2011-11-03 151 views
2

我正在寻找一种解决方案,以使用来自csv的我的表中的交替颜色行设计。交替行颜色

我的代码:

<?php 
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n 
<thead> 
<tr> 
<th>data</th> 

</tr> 
</thead>"; 
$f = fopen("local/datafiles.csv", "r"); 
while (($line = fgetcsv($f)) !== false) { 
     echo "<tr class='odds'>"; 
     foreach ($line as $cell) { 
         echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>"; 
     } 
     echo "</tr>\n"; 
} 
fclose($f); 
echo "\n</table>"; 
?> 

所以,我怎么能有TR类从赔率甚至交替? 感谢

+0

它与csv无关。 – onatm

回答

2
<?php 
echo "<table style='text-align: left; width: 99%;' border='1'>\n\n 
<thead> 
<tr> 
<th>data</th> 

</tr> 
</thead>"; 
$f = fopen("local/datafiles.csv", "r"); 
$i = 0; 
while (($line = fgetcsv($f)) !== false) { 
    echo "<tr style='background-color: ".(($i%2)?'green':'blue').";'>"; 
    foreach ($line as $cell) { 
     echo "<td style='font-weight: bold;'>" . htmlspecialchars($cell) . "</td>"; 
    } 
    echo "</tr>\n"; 
    $i++; 
} 
fclose($f); 
echo "\n</table>"; 
?> 

适应,或者你可以分配一个类与使用css的方式和样式相同。这是更好的做法。

+0

Thyks OptimusCrime这很明显,我需要 – meandme

3

具有可变

诠释计数= 0;在每次迭代

增量次数并采取modolous通过

计数%2

if(count % 2 == 0) 
color = red //(make color of row = red) 

else 
color = yellow //(make color of row = yello) 

count=count+1; 

那只是一个想法,美国可以在我们的代码

2

你可以使用CSS伪选择:第n-的类型(奇|偶)

http://reference.sitepoint.com/css/pseudoclass-nthoftype

如果表中的ID是styledTable,你的样式看起来像:

#styledTable { 
text-align: left; 
width: 99%; 
border: 1px solid black; 
} 

#styledTable tr:nth-of-type(even) { 
background-color: red; 
} 

#styledTable tr:nth-of-type(odd) { 
background-color: blue; 
} 
2

我会给你一个更好,但更简单的解决方案,通过CSS。首先唯一标识你的桌子,我会告诉你一个更普遍的选择。

table tr { backgroun: #ddd; } 
table tr:nth-child(2n) { background: #ccc; } 
+0

我知道,这不是一个CSS问题,但它应该是 – Starx