Php – fetching thesql data with multi color with php

php

how can i fetch data from mysql with php and display it in mutliple color….like

[gray]first post
[white]second post
[gray]third post
[white]forth post

i know mysql_fetch_array but how can i display data with multi color like above

Best Solution

You have to loop over your lines, knowing if you are on an "even" or "odd" line (at least, if you want two colors), which can be calculated using the modulo operator.

For instance, as a quick idea, the following code :

$arr = array(
  'first',
  'second',
  'third',
  'fourth',
);

$i = 0;
foreach ($arr as $line) {
  $class = ($i%2 ? 'odd' : 'even');
  echo '<div class="' . $class . '">' . htmlspecialchars($line) . '</div>' . "\n";
  $i++;
}

Will give this HTML output :

<div class="even">first</div>
<div class="odd">second</div>
<div class="even">third</div>
<div class="odd">fourth</div>

And now, up to you to configure the two .odd and .even CSS classes to get the colors you want.