具有交替行颜色的PHP代码

时间:2011-02-02 14:18:05

标签: php

你好,我是编程新手。我想使用备用行颜色创建一个表。但不知道该怎么做。这是我的代码。请帮帮我!

while ($row = mysqli_fetch_assoc($result)) {
    echo '<tr>';
    echo '<td>' . $row['a.ServiceID'] . '</td>';
    echo '<td>' . $row['a.Title'] . '</td>';
    echo '<td>' . $row['a.Description'] . '</td>';
    echo '<td>' . $row['a.Notes'] . '</td>';
    echo '<td>' . $row['a.SubmitBy'] . '</td>';
    echo '<td>' . $row['a.AssignedEmp'] . '</td>';
    echo '<td>' . $row['c.GroupName'] . '</td>';
    echo '<td>' . $row['d.NameCategory'] . '</td>';
    echo '<td>' . $row['e.TipoStatus'] . '</td>';
    echo '<td>' . $row['f.TiposUrgencia'] . '</td>';
    echo '<td>' . $row['g.CustomerName'] . '</td>';
    echo '<td>' . $row['a.DayCreation'] . '</td>';
    echo '<td>' . $row['a.ModifyBy'] . '</td>';
    echo '<td>' . $row['a.ModifyTime'] . '</td>';
    echo '</tr>';
}

mysqli_free_result($result);
echo '</table>';

6 个答案:

答案 0 :(得分:4)

$rowColors = Array('#FF0000','#00FF00'); $nRow = 0;
while ($row = mysqli_fetch_assoc($result)){
  echo '<tr style="background-color:'.$rowColors[$nRow++ % count($rowColors)].';">';
  // ....
  echo '</tr>';
}

或者可以编辑它以应用类。只需将类名放在$ rowColors中,然后将echo更改为<tr class="'.$rowColors[...].'">

可以找到工作示例here

答案 1 :(得分:4)

$c = false;
while ($row = mysqli_fetch_assoc($result)) {
    echo '<tr style="background:',(($c=!$c)? '#eee' : '#ddd' ),'">';
    // ...
}

或者使用CSS 3:

tr:nth-child(odd){ background:#eee; }
tr:nth-child(even){ background:#ddd; }

答案 2 :(得分:1)

$rowColors = Array('#FFFFFF','#FF0000'); $i= 0;
while ($row = mysqli_fetch_assoc($result)) 
{
    echo '<tr style="background-color:'.$rowColors[$i++ % count($rowColors)].';">';
    echo '<td>' . $row['a.ServiceID'] . '</td>';
    echo '<td>' . $row['a.Title'] . '</td>';
    echo '<td>' . $row['a.Description'] . '</td>';
    echo '<td>' . $row['a.Notes'] . '</td>';
    echo '<td>' . $row['a.SubmitBy'] . '</td>';
    echo '<td>' . $row['a.AssignedEmp'] . '</td>';
    echo '<td>' . $row['c.GroupName'] . '</td>';
    echo '<td>' . $row['d.NameCategory'] . '</td>';
    echo '<td>' . $row['e.TipoStatus'] . '</td>';
    echo '<td>' . $row['f.TiposUrgencia'] . '</td>';
    echo '<td>' . $row['g.CustomerName'] . '</td>';
    echo '<td>' . $row['a.DayCreation'] . '</td>';
    echo '<td>' . $row['a.ModifyBy'] . '</td>';
    echo '<td>' . $row['a.ModifyTime'] . '</td>';
    echo '</tr>';
}

mysqli_free_result($result);
echo '</table>';

答案 3 :(得分:1)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/view.css">
<title>ShowList</title>
</head>

<body>
<table  width="100%"  height="830" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td  colspan="3" align="left">
    <?php include 'common/header.html'; ?>
    </td>
    </tr>
    <tr>
    <td colspan="3">
    <?php include 'sepperatemunu.php'; ?>   
    </tr>
    <tr height="720" width="1240" align="center" valign="middle">
    <td>
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('Could not Connect'.mysql_error());
}
mysql_select_db("USER", $con);
$color="1"; 
$row_count = 0;
$sql=mysql_query("select * from registration");
echo "<table border='1' width='100%' height='400' cellpadding='0' cellspacing='0' >
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>DOB</th>
<th>Gender</th>
<th>MobileNo</th>
<th>Address</th>
<th>country</th>
<th>Modify</th>
</tr>";
?>
<?php
while($row=mysql_fetch_array($sql) )
{
if($color==1)
{
echo "<tr bgcolor='#FFFFFF'>";
  echo "<td align='center'>" . $row['ID'] . "</td>";
  echo "<td align='left'>" . $row['Name'] . "</td>";
  echo "<td align='center'>" . $row['Age'] . "</td>";
  echo "<td align='center'>" . $row['DOB'] . "</td>";
  echo "<td align='center'>" . $row['Gender'] . "</td>";
  echo "<td align='center'>" . $row['MobileNo'] . "</td>";
  echo "<td align='left'>" . $row['Address'] . "</td>";
  echo "<td align='center'>" . $row['Country'] . "</td>";
  echo "<td align='center'>" ?><a href='Datails.php?edit=<?php echo $row['ID'];?>' class="link_class">edit</a> <?php "</td>";
  echo "</tr>";
$color="2";
}
else 
{
echo "<tr bgcolor='#808080'>";
echo "<td align='center'>" . $row['ID'] . "</td>";
  echo "<td align='left'>" . $row['Name'] . "</td>";
  echo "<td align='center'>" . $row['Age'] . "</td>";
  echo "<td align='center'>" . $row['DOB'] . "</td>";
  echo "<td align='center'>" . $row['Gender'] . "</td>";
  echo "<td align='center'>" . $row['MobileNo'] . "</td>";
  echo "<td align='left'>" . $row['Address'] . "</td>";
  echo "<td align='center'>" . $row['Country'] . "</td>";
  echo "<td align='center'>" ?><a href='Datails.php?edit=<?php echo $row['ID'];?>' class="link_class">edit</a> <?php "</td>";
  echo "</tr>";
$color="1";
}  
}
echo "</table>";
mysql_close($con);
?>
    </td>
    </tr>
     <tr>
    <td colspan="3" align="left">
    <?php include 'common/footer.html'; ?>
    </td>
  </tr>
</table>
</body>
</html>

答案 4 :(得分:0)

使用一些二进制变量将行颜色状态存储在:

$colored = false;
while($row = mysqli_fetch_assoc($result)) {

  // depending on the state of colored, choose color:
  if($colored)
    echo '<tr style=\"background-color:lightgray;\">';
  else
    echo '<tr style=\"background-color:white;\">'; // or '<tr>';

  // change the state of $colored:
  $colored = !$colored;

  echo '<td>' . ...
     ...
  echo '</tr>';
}

您可以将样式信息放在css中(例如,在两个类“backgroundbright”和“backgroundlow”中)并将这些定义添加到您的trs中:

  // depending on the state of colored, choose color:
  if($colored)
    echo '<tr class=\"backgroundlow\">';
  else
    echo '<tr class=\"backgroundbright\">'; // or '<tr>';

答案 5 :(得分:0)

我想你想要像

这样的东西
$num = 0;
while ($row = mysqli_fetch_assoc($result)) {
    $color= ($num % 2 == 0) ? 'color1' : 'color2';
    $num++;
    echo '<tr style="background-color:'.$color.';">';
    echo '<td>' . $row['a.ServiceID'] . '</td>';
    echo '<td>' . $row['a.Title'] . '</td>';
    echo '<td>' . $row['a.Description'] . '</td>';
    echo '<td>' . $row['a.Notes'] . '</td>';
    echo '<td>' . $row['a.SubmitBy'] . '</td>';
    echo '<td>' . $row['a.AssignedEmp'] . '</td>';
    echo '<td>' . $row['c.GroupName'] . '</td>';
    echo '<td>' . $row['d.NameCategory'] . '</td>';
    echo '<td>' . $row['e.TipoStatus'] . '</td>';
    echo '<td>' . $row['f.TiposUrgencia'] . '</td>';
    echo '<td>' . $row['g.CustomerName'] . '</td>';
    echo '<td>' . $row['a.DayCreation'] . '</td>';
    echo '<td>' . $row['a.ModifyBy'] . '</td>';
    echo '<td>' . $row['a.ModifyTime'] . '</td>';
    echo '</tr>';
}

mysqli_free_result($result);
echo '</table>';