tags:

views:

90

answers:

4
<table>
while ($row = mysql_fetch_assoc($result)) {
<tr>
<td>
echo $row['test'] . " " . ' ($' . $row['test2'] . ")<br>";
</td>
</tr>
}
</table>

How do I make a pattern for the background color? For ex, grey, blue, grey blue.

+2  A: 

There are multiple ways of doing this. Here is one.

<table>
<?php $i = 0; ?>
<?php while ($row = mysql_fetch_assoc($result)): ?>
<tr<?php echo (++$i & 1 == 1) ? ' class="odd"' : '' ?>>
<td>
<?php echo $row['test'] . " " . ' ($' . $row['test2'] . ") ?><br>
</td>
</tr>
<?php endwhile; ?>
</table>

I suggest giving a CSS class (I've called it "odd" here) to every second row rather than both odd and even. Then you just do:

tr td { background: grey; }
tr.odd td { background: blue; }

in CSS.

cletus
+1  A: 

If it's a 2 colour pattern, use a variable to switch between blue and grey. if more than 2 colours, use a rotating counter

2 colours

$blue = true;
<table>
while ($row = mysql_fetch_assoc($result)) {
<tr>
<td color="<?php echo $blue?'blue':'grey'; $blue ^= true; ?>">
echo $row['test'] . " " . ' ($' . $row['test2'] . ")<br>";
</td>
</tr>
}
</table>

More than 2 colours, the general solution:

$colourIndex = 0;
$colours = ('blue', 'red', 'green');

...
...

<td color="<?php echo $colours[$colourIndex]; $colourIndex = ($colourIndex+1)%length($colours); ?>">
Charles Ma
`$blue = !$blue` is a less _clever_ method.
Ollie Saunders
What's a rotating counter?
Doug
($counter + 1)%n where n is the number of colours you have :P
Charles Ma
haha Ollie, I used xor because in my experience, it's normally used to flip a bit, while $blue = !$blue looks a bit confusing...even philosophical, "blue is not blue", "2b|!2b", "i.think() => i.am()"
Charles Ma
Clarification: It's a "rotating counter" because it "rotates" back to start after it counts to the last number of an interval (interval being a sequence of numbers with a start and an end number).
Spoike
@Charles Ma: I usually tell early programming students that "=" is not an equals sign. Instead it is "this is assigned with this" in order to clear up any confusion between the semantics of "=" and "==" in C-like languages.
Spoike
@Charles It's not philosophical because it's not a statement of truth, like it would be in a functional language, it's a sequence of operations: 1) get $blue, 2) invert truth of got value, 3) assign new value to $blue.
Ollie Saunders
Yes, I understand that. My point was that it feels more natural to me to use xor rather than ! in that particular situation because of my previous experiences :P
Charles Ma
+1  A: 

You need something like a state variable, where you store wheter the last row was blue or grey. Then you print out the other color and update the state variable for the next pass.

This is one example:

<?php

echo '<table>';

$state = 1;
while ($row = mysql_fetch_assoc($result)) {
 echo '<tr>';
 if( $state%2 == 0 )
  echo '<td style="background-color:grey;">';
 else
  echo '<td style="background-color:blue;">';
 echo $row['test'] . " " . ' ($' . $row['test2'] . ")<br>";
 echo '</td></tr>';
 $state++;
}
echo '</table>';

?>
svens
A: 

You can also use the InfiniteIterator to repeat the sequence again and again. This, like the "rotating counter", works for an arbitrary amount of elements.

$props = new InfiniteIterator(
  new ArrayIterator(array('a', 'b', 'c','d', 'e'))
); $props->rewind();

$l = rand(10, 20); // or whatever
for ($i=0; $i<$l; $i++) {
  $p = $props->current(); $props->next();
  echo 'class="', $p, '"... ';
}
VolkerK
This is so complex!
Ollie Saunders
Really? I find this solution easier to read than the others. And easier to tweak.
chendral