MySQLi PHP使用Modulo来交替排样式

I have a fairly simple query in which I'd like to alternate styling of table rows. I have some logic mixed up in the while and for loop most likely but I'm stuck on how to correct it.

<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>

Easiest way would be to use even and odd CSS property in your case

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

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