如何为php中的迭代表设置交替行颜色? [英] How to set alternate row color for an iterated table in php?

查看:144
本文介绍了如何为php中的迭代表设置交替行颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP和我迭代一个表结果数组...我想添加行颜色和交替行颜色....如何这样做?任何建议...

 < table id =chkboxcellpadding =0cellspacing =2
width =100%class =table_Style_Border>
< tr>
< td style =width:150pxclass =grid_headeralign =center> RackName< / td>
< td style =width:150pxclass =grid_headeralign =center> LibraryName< / td>
< td style =width:200pxclass =grid_headeralign =center> LibrarianName< / td>
< td style =width:200pxclass =grid_headeralign =center> Location< / td>
< td style =width:1%class =grid_header>< / td>


< / tr>

< ;? if(isset($ comment))
{echo'< tr&
< td class = table_label colspan = 5>'。$ comment。'< / td>< / tr>'; }?>
<?php foreach($ rackData as $ row){?>
< tr>
< td align =leftclass =table_label>
<?= $ row ['rack_name']?>
< / td>
< td align =leftclass =table_label>
<?= $ row ['library_name']?>
< / td>
< td align =centerclass =table_label>
<?= $ row ['librarian']?>
< / td>
< td align =centreclass =table_label>
<?= $ row ['location']?>
< / td>
< td align =center>
< input type =checkboxname =groupid =group
value =<?= $ row ['rack_id']? onclick =display(this); >
< / td>

< / tr>

< ;? }?>
< table>

编辑

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

似乎没有你的语法....



错误:

 解析错误:语法错误,在D:\xampp\htdocs\codeigniter_cup_myth_new\system\\\\\\\\\\\\\\\\\\\部署b 

解决方案

使用 modulo

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

然后你可以定义CSS类$ c> .odd .even ,并给予 background-color



使用现代浏览器(阅读:不是IE 8或更低版本),您也可以直接在CSS中使用:nth-​​child pseudo class

  tr:nth- :#FFF; } 
tr:nth-​​child(odd){background-color:#EEE; }


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


    </tr>

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

    </tr>

  <?    } ?>
    <table>

EDIT:

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

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

ERROR:

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

解决方案

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

这篇关于如何为php中的迭代表设置交替行颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆