简体   繁体   English

如何在php中为迭代表设置备用行颜色?

[英]How to set alternate row color for an iterated table in php?

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? 我正在使用PHP,并且正在迭代带有结果数组的表...我想向其添加行颜色和备用行颜色。 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 解析错误:语法错误,第238行的D:\\ xampp \\ htdocs \\ codeigniter_cup_myth_new \\ system \\ application \\ views \\ rackdetails.php中出现意外的':'

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. 然后,你可以定义CSS类,名称分别.odd.even ,并给予他们的background-color ,你要对行与交替。

With modern browsers (read: not IE 8 or lower) you can also do it directly in CSS with the :nth-child pseudo class : 使用现代浏览器(阅读:不是IE 8或更低版本),您也可以直接在CSS中使用:nth-​​child伪类

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

To streamline your server code you could use javascript to highlight your rows and add mouse over/out handlers to the rows to do whatever you want. 要简化服务器代码,您可以使用javascript突出显示行,并在行上添加鼠标悬停处理程序以执行所需的操作。

Very easy to do with jquery and many examples. 使用jquery和许多示例非常容易。

<?php 
include("BLL/index.php");
$objBLL = new BLL();
$result = $objBLL->SelectQuery();
$ID = $i;
$i = 1;
?>
<table border="1" width="50%">
    <tr>
        <td>ID</td>
        <td>NAME</td>
        <td>DESCRIPTION</td>
    </tr>   

<?php
 while ($row = mysql_fetch_assoc($result)) {
  if ($i % 2 != 0) {# An odd row 
    $rowColor = "orange"; 
    echo '<tr bgcolor="' . $rowColor . '"><td >' . $row["ID"] . '</td><td >' . $row["Name"] . '</td><td >' . $row["Description"] . '</td></tr>' . "\r\n";
    $i++;
  }else{  # An even row 
    $rowColor = "green";
    echo '<tr bgcolor="' . $rowColor . '"><td >' . $row["ID"] . '</td><td >' . $row["Name"] . '</td><td >' . $row["Description"] . '</td></tr>' . "\r\n";
    $i++;
  } 
}
?>  
</table>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM