0

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>'; 
3
  • You need to apply alternate styling using CSS. Are you familiar with CSS? Commented Feb 2, 2011 at 14:19
  • Elaborate. Do you mean using nth-child CSS3 or simply susing css classes instead of inline styles. If the former provide and example maybe to illustrate what you mean? Commented Feb 2, 2011 at 14:44
  • I meant about using CSS classes. Commented Feb 3, 2011 at 6:13

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

1 Comment

1+ Nice interesting PHP usage, much better than a CSS class ;)
4
$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; } 

Comments

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

Comments

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> 

1 Comment

full code to fetch the datafrom database in a tabular form and display alternate row in same color
0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.