简体   繁体   English

PHP代码具有交替的行颜色

[英]PHP code with alternating row color

Hello Im new in programing. 您好,我是编程新手。 I want to create a table using the alternate row color. 我想使用替代行颜色创建一个表。 But dont know how to do it. 但是不知道该怎么做。 Here is my code. 这是我的代码。 Please help me! 请帮我!

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

Or this could be edited to apply classes. 或者可以编辑它以应用类。 Just place the class names in $rowColors, and change the echo to <tr class="'.$rowColors[...].'"> instead. 只需将类名称放在$ rowColors中,然后将回显更改为<tr class="'.$rowColors[...].'">

Working example can be found here . 工作示例可以在这里找到。

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

Or with CSS 3: 或使用CSS 3:

tr:nth-child(odd){ background:#eee; }
tr:nth-child(even){ background:#ddd; }
<!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>
$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>';

Use some binary variable to store the row color state in: 使用一些二进制变量将行颜色状态存储在:

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

You can place the style information in your css (eg in two classes "backgroundbright" and "backgroundlow") and add these definitions to your trs: 您可以在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>';

I think you want something like 我想你想要类似的东西

$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>';

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

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