maltad
maltad

Reputation: 7

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

Upvotes: 0

Views: 5236

Answers (6)

Vikas Gehlot
Vikas Gehlot

Reputation: 11

<!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>

Upvotes: 1

vipin soni
vipin soni

Reputation: 11

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

Upvotes: 1

Martin Hennings
Martin Hennings

Reputation: 16866

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 (e.g. in two classes "backgroundbright" and "backgroundlow") and add these definitions to your trs:

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

Upvotes: 0

Saikios
Saikios

Reputation: 3721

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

Upvotes: 0

Floern
Floern

Reputation: 33914

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

Or with CSS 3:

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

Upvotes: 4

Brad Christie
Brad Christie

Reputation: 101614

$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.

Working example can be found here.

Upvotes: 4

Related Questions