开发者

display data in grid using php

开发者 https://www.devze.com 2023-02-12 02:15 出处:网络
I am trying to display the retrieved data in grid. I used while loop and retrieved every thing.I applied css but now all rows are getting displayed in same color.

I am trying to display the retrieved data in grid.

I used while loop and retrieved every thing.I applied css but now all rows are getting displayed in same color.

I am trying to do like this row0 blue color row1 green color row2 bl开发者_运维百科ue color row3 green color.How do i do it?


Use something like this (of course, adapt it to your loop) :

echo '<table>';
$i = 0;
foreach($myrows as $row) {
    echo '<tr class="row'.(++$i % 2).'"><td>';
    echo $row;
    echo '</td></tr>';
}
echo '</table>';

With the following CSS code :

tr.row0 > td { background-color: blue; }
tr.row1 > td { background-color: green; }


Few methods if happy using CSS3 look at nth-child

Or place a counter inside your while statement and use mod function to apply a class

e.g.

while(...){

if($count%2)
{
 \\add class 
}
$count++;
}


You can do it in CSS3 (see http://www.w3.org/Style/Examples/007/evenodd) In PHP, you can apply a "odd" class using a modulo:

<?php for ($i = 0; $i < 10; $i++) {
$class = ($i%2 != 0) ? ' class="odd"' : '';
echo '<tr'.$class.'>...</tr>';
}


You can also use a free tool like SDTable.com and it will do the job for you. You just go in into template css file and change row-1 and row-2 background colors or create a rule which specify the background color dynamicly based on the native data in your database.

0

精彩评论

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