簡體   English   中英

MySQLi PHP使用Modulo替代行樣式

[英]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;?>&nbsp;&nbsp;&nbsp;
            </td>
            <td>
            <?php echo $row->Author;?>&nbsp;&nbsp;&nbsp;
            </td>
            <td>
            <?php echo $row->Media; ?>
            </td>
        </tr> 
   <?php     
            }   
       }
   }
  ?>
  </table>

最簡單的方法是在您的情況下使用evenodd CSS屬性

tr:nth-child(even) {background: #CCC;}
tr:nth-child(odd) {background: red;}

http://www.w3.org/Style/Examples/007/evenodd.en.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM