Reputation:
I have 15 or more rows in mysql database. I want to retrieve them and display in 2 different colors.
For First row <tr><td height="30" bgcolor="#F5F5F5">....</td></tr>
For second row <td height="30" align="center" bgcolor="#FFFFFF">....</td></tr>
For third row <td height="30" bgcolor="#F5F5F5">....</td></tr>
For Forth row <td height="30" align="center" bgcolor="#FFFFFF">....</td></tr>
And so on....
How to display them in such order using php
Upvotes: 1
Views: 67
Reputation: 1
You first need to fetch rows from database in your php script and then in php you can loop these rows and apply an odd or even class, I have made an example script using php array. In your case $items will contain rows fetched from DB.
$items = array("abc", "123", "def", "345", 'wer');
foreach($items as $key => $val) {
if($key == 0 ) {
$class = 'odd';
}
elseif($key%2 == 0) {
$class = 'odd';
}
else {
$class = 'even';
}
echo $class . " $val" ."<br/>";
}
Let me know if it help you.
Upvotes: 0
Reputation: 78
You can use CSS selectors:
tr:nth-child(even) {
background: #F5F5F5;
}
tr:nth-child(odd) {
background: #FFF;
}
Sample: http://www.w3schools.com/cssref/tryit.asp?filename=trycss3_nth-child_odd_even
Upvotes: 3