开发者

How to give alternating table rows different background colors using PHP

开发者 https://www.devze.com 2023-01-03 13:38 出处:网络
I have a table of data that is generated dynamically based on the contents stored in a mysql database.

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:

<tabl开发者_如何转开发e 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?


Or you could just use CSS:

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


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


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++;
}


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


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

`


This is how I did it. I declared a css class called "even" with all the styling i wanted. Then looped through the scenario. Hope it helps!

<?php
    include 'connect.php';
    echo "<table id='hor-zebra'>";
    $i = 0;
    while($row = mysql_fetch_array($result))
    {
       if($i%2 == 0)
       {
          echo "<tr class='even'>";
          echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
          echo "</tr>";
       }

       else
       {
          echo "<tr>";
          echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
          echo "</tr>";
       }
       $i++;
    }
    echo "</table>";

    mysql_close($con);

  ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消