tags:

views:

110

answers:

4

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

A: 

In the loop

$i=0;
foreach ($rows as $row){
  echo '<tr class="color'.($i%2).'">.........';
  $i++;
}

in the css

.color1{ background-color: white}
.color0{ background-color: silver}
Itay Moav
+1  A: 

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.

Pascal MARTIN
I'd reverse the modulo: it's a bit weird to have the first line "even" and the second one "odd".
sprugman
@sprugman : maybe, yes ^^ doesn't change much, but it might feel more logical ; depends if you consider that "first" line is line 1 or line 0, I suppose
Pascal MARTIN
but what should i use as $arr i.e i has $row=mysql_fetch_array($sql)did you mean this $row
testkhan
+1  A: 

There are many ways to do this (most of them more sophisticated than this example) but you're seemingly looking for a simple solution.

for($c=true; false!==($row=mysql_fetch_array($result, MYSQL_ASSOC)); $c=!$c) {
  $class = $c ? 'even':'odd';
  echo '<div class="', $class, '">', $row['x'], '</div>', "\n";
}

$c ? 'even':'odd'; tests whether $c is true or false and the result is 'even' if it is true and 'odd' if it is false. I.e. $class is either 'even' or 'odd' depending on the value of $c.
The for-loop starts with $c=true (initialization part of the for-loop). At the end of each iteration of the loop $c=!$c is executed which flips $c from true->false or false->true, i.e. $c alternates between true and false.

You can also use javascript to let the client add the coloring as an enhancement.
E.g. using jquery:

<html>
  <head>
    <style type="text/css">
      tr.even td { background-color: red; }
      tr.odd td { background-color: blue; }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
      $(document).ready( function() {
        $('#t1 tr:odd').addClass('even');
        $('#t1 tr:even').addClass('odd');
      });
    </script>
  </head>
  <body>
    <table id="t1">
      <tr><td>a</td><td>A</td></tr>
      <tr><td>b</td><td>B</td></tr>
      <tr><td>c</td><td>C</td></tr>
      <tr><td>d</td><td>D</td></tr>
      <tr><td>e</td><td>E</td></tr>
    </table>
  </body>
</html>
VolkerK
A: 

Itay Moav's and Pascal MARTIN's examples work, but if you have a numeric array you can simplify the code slightly:

foreach ($rows as $i => $row){
    $even_odd = ($i%2) ? 'even' : 'odd';
    echo "<tr class='$even_odd'>......";
}
sprugman