Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give alternating table rows different background colors using PHP

Tags:

php

I have a table of data that is generated dynamically based on the contents stored in a mysql database.

This is how my code looks:

<table border="1">
    <tr>
        <th>Name</th>
        <th>Description</th>
        <th>URL</th>
    </tr>
    <?php
        $query = mysql_query("SELECT * FROM categories");
        while ($row = mysql_fetch_assoc($query)) {
            $catName = $row['name'];
            $catDes = $row['description'];
            $catUrl = $row['url'];

            echo "<tr class=''>";
            echo "<td>$catName</td>";
            echo "<td>$catDes</td>";
            echo "<td>$catUrl</td>";
            echo "</tr>";
        }
    ?>
</table>

Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:

.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }

and that would be the end of that. However since the table rows are dynamically generated, how can I achieve this?

like image 443
Sam Avatar asked Jun 14 '10 00:06

Sam


Video Answer


5 Answers

Or you could just use CSS:

table tr:nth-child(odd) { background-color: #ccc; }

like image 162
Jason Palmer Avatar answered Nov 15 '22 22:11

Jason Palmer


<?php 

$x++; 

$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';

echo "<tr class='$class'>";

?>

It basically checks to see if $x is divisible evenly by 2. If it is, it is even.

P.S. if you haven't seen that style of if else query, it is called a ternary operator.

like image 42
Aaron Harun Avatar answered Nov 15 '22 20:11

Aaron Harun


Set a variable to true/false or a number and then back again during each iteration. Or use the modulus operator such as $i%2==0 in a while loop where $i is a number and use this condition in a ternary statement or something that sets the class value of the <tr>

Easiest way to alternate row colors in PHP/HTML?

$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
 echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
like image 41
meder omuraliev Avatar answered Nov 15 '22 20:11

meder omuraliev


<?
$color="1";
while ($line = mysql_fetch_array($result)) {
  if($color==1){
    echo '<tr bgcolor="">';
    $color="2";
  } else { 
    echo '<tr bgcolor="#dcdcdc">';
    $color="1";
  }
  ?><td align="left" width="40"><a href=""></a><?= $line[name] ?></td>

<?
}
?>

This is my working code !

like image 44
viorel Avatar answered Nov 15 '22 20:11

viorel


Here is my working part ! `

 $i=1;
 while($row = mysqli_fetch_array($result)) {
 if($i%2==0)
 {
     echo '<tr bgcolor="#FFFF00">';
 }
 else
 {
     echo '<tr bgcolor="#99FFCC">';
 }
     $i++;
     echo "<td>" . $row['blah'] . "</td>";
     echo "<td>" . $row['blah_blah'] . "</td>";
     echo "</tr>";

 }
 echo "</table>";

`

like image 34
YakuZa Avatar answered Nov 15 '22 21:11

YakuZa