PHP IF-statement to change color of table rows <td>?
I have the following code written below. With this code I want to fetch data from MySQL database and print(echo) the result in table rows, each row with different colour. With this code data is fetched from database successfully but my condition for change color of rows is not working. Please check my code and let me know where I'm wrong.
<table width="500" align="center" style="border:1px solid;">
<?php
$query = "SELECT * FROM main_cat";
$result = mysql_query($query);
$count = mysql_num_rows($result);
while($end = mysql_fetch_array($result)){
echo "<tr>";
if($count % 2 == 0){
echo '<td bgcolor="#CCCCCC">'.$end['page_name'].'</td>';
}else{
echo '&l开发者_JAVA技巧t;td bgcolor="#99CC66">'.$end['page_name'].'</td>';
}
echo "</tr>";
}
?>
</table>
You're not initializing $count
to 0 and you're not incrementing $count
anywhere.
This should work:
<table width="500" align="center" style="border:1px solid;">
<?php
$query = "SELECT * FROM main_cat";
$result = mysql_query($query);
$i = 0;
while($end = mysql_fetch_array($result)){
echo "<tr>";
if($i % 2 == 0){
echo '<td bgcolor="#CCCCCC">'.$end['page_name'].'</td>';
}else{
echo '<td bgcolor="#99CC66">'.$end['page_name'].'</td>';
}
echo "</tr>";
$i++;
}
?>
</table>
Even better would be using CSS, namely the odd and even selectors: http://www.w3.org/Style/Examples/007/evenodd
You need to put a count in the while loop rather than the total number returned by the query.
Not going to write the code for you.
Before while loop set counter to zero.
In while loop check condition of counter Then increase counter value by 1
If you like, you can shorten this code, and get the HTML out of the PHP code. This will allow editors like Notepad++ or NetBeans to highlight the HTML code too. Besided, you got that line of HTML only once, so it will be easier to maintain.
However, it is smartest to use css to style the alternating rows, or at least use a class to style the individual rows.
I used inline styling in my code which is dirty as it is. But bgcolor. Really? It is not even supported anymore in HTML 4.1 strict. It is old, it is icky.
<table width="500" align="center" style="border:1px solid;">
<?php
$query = "SELECT * FROM main_cat";
$result = mysql_query($query);
$i = 0;
while($end = mysql_fetch_array($result)){
$color = ($i++ % 2 == 0 ? '#CCCCCC' : '#99CC66');
$page = $end['page_name'];
?>
<tr><td style="background-color: <?=$color?>"><?=$page?></td></tr>
<?php
}
?>
</table>
Credit goes to Jeroen. It is his code I based this example on.
i think you should make a array of different colors and then change color of each row with change index. like this
<table width="500" align="center" style="border:1px solid;">
<?php
$query = "SELECT * FROM main_cat";
$result = mysql_query($query);
$clr=0;
$colors = array('blue','green','purple','orange','red');
while($end = mysql_fetch_array($result)){
echo '<tr style="font-size:18px;background-color:'.$colors[$clr].'">';
echo '<td bgcolor="#CCCCCC">'.$end['page_name'].'</td>';
echo '<td bgcolor="#99CC66">'.$end['page_name'].'</td>';
echo "</tr>";
$clr++;
if($clr==5)
$clr=0;
}
?>
精彩评论