2010-04-03 10:21
浏览 26


I am using PHP and I am iterating a table with a result array ... I want to add row color and alternate row color to it.... How to do so? Any suggestion...

<table  id="chkbox" cellpadding="0" cellspacing="2" 
               width="100%" class="table_Style_Border">
<td style="width:150px" class="grid_header" align="center">RackName</td>    
   <td style="width:150px" class="grid_header" align="center">LibraryName</td>  
<td style="width:200px" class="grid_header" align="center">LibrarianName</td>
<td style="width:200px" class="grid_header" align="center">Location</td>
        <td style="width:1%" class="grid_header"></td>


     <? if(isset($comment))
           { echo '<tr>
      <td class=table_label colspan=5>'.$comment.'</td></tr>'; } ?>
    <?php foreach($rackData as $row) { ?>
        <td align="left" class="table_label">
        <td align="left" class="table_label">
        <td align="center" class="table_label">
        <td align="center" class="table_label">
        <td align="center">
            <input type="checkbox" name="group" id="group" 
  value="<?=$row['rack_id']?>" onclick="display(this);"  > 


  <?    } ?>


<?php foreach($rackData as $key =>  $row) { ?>
        <?php printf('<tr class="%s">', ($key % 2) ? : 'rowcolor' : 'alternaterowcolor');?>

It doesn't seem to take your syntax....


Parse error: syntax error, unexpected ':' in D:\xampp\htdocs\codeigniter_cup_myth_new\system\application\viewsackdetails.php on line 238

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • dougao7801 2010-04-03 10:23

    Use modulo

    <?php foreach($rackData as $key => $row) { ?>
        <?php printf('<tr class="%s">', ($key % 2) ? 'odd' : 'even'); ?>
        // ...

    Then you can define CSS classes with the names .odd and .even and given them the background-color you want the rows to alternate with.

    With modern browsers (read: not IE 8 or lower) you can also do it directly in CSS with the :nth-child pseudo class:

    tr:nth-child(even) { background-color: #FFF; }
    tr:nth-child(odd) { background-color: #EEE; }
    解决 无用
    打赏 举报

相关推荐 更多相似问题