[英]MySQLi PHP using Modulo to alternate row style
我有一個相當簡單的查詢,我想在其中替換表行的樣式。 在while和for循環中,我很可能混淆了一些邏輯,但我仍在糾結中。
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Media Type</th>
</tr>
<?php
if($result = $link->query("SELECT * FROM smallgroup order by Author")){
if($result->num_rows) {
while($row = $result->fetch_object()){
for($i=0;$i<10;$i++){
if($i % 2)
{
?>
<tr style="background-color:#ccc;">
<?php
}else{
?>
<tr style="background-color:red;">
<?php
}
}
?>
<td>
<?php echo $row->Title;?>
</td>
<td>
<?php echo $row->Author;?>
</td>
<td>
<?php echo $row->Media; ?>
</td>
</tr>
<?php
}
}
}
?>
</table>
最簡單的方法是在您的情況下使用even
和odd
CSS屬性
tr:nth-child(even) {background: #CCC;}
tr:nth-child(odd) {background: red;}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.