By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

How can I change every second row?

P: n/a
Hi

I am making a html table builder, and would like every second row to be
blue. So for that purpose I need a number that changes on every
iteration of a while loop, for exampel so that a value is 0 then 1 then
0 a so forth.

Any suggestions ?

Mads

Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Well, if you get the results with

while ($row = mysql_fetch_assoc($result)){}

you could have a integer variable defined before the loop

$x = 0;
while ($row = mysql_fetch_assoc($result)){
if ($x % 2 == 0) blue
else ...
}


ma************@gmail.com wrote:
Hi

I am making a html table builder, and would like every second row to be
blue. So for that purpose I need a number that changes on every
iteration of a while loop, for exampel so that a value is 0 then 1 then
0 a so forth.

Any suggestions ?

Mads

Jul 17 '05 #2

P: n/a
Thanks a lot, I totally cool.

I had to ad $x=$x+1;

but perhaps there is another way?

Jul 17 '05 #3

P: n/a
I noticed that Message-ID:
<11*********************@g47g2000cwa.googlegroups. com> from
ma************@gmail.com contained the following:

I had to ad $x=$x+1;

but perhaps there is another way?


$x++;

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #4

P: n/a
Sorry, forgot that :)

Geoff Berrow wrote:
I noticed that Message-ID:
<11*********************@g47g2000cwa.googlegroups. com> from
ma************@gmail.com contained the following:

I had to ad $x=$x+1;

but perhaps there is another way?

$x++;

Jul 17 '05 #5

P: n/a
>>> I had to add $x=$x+1;

but perhaps there is another way?

$x++;


Some other approaches:

$x = 0;
if ( ++$x % 2 )
{
...
}

would be even shorter, or:

$x = 0;
if ( $x = ~$x )
{
...
}

would also work.

--
Martijn
http://www.sereneconcepts.nl
Jul 17 '05 #6

P: n/a
Q: How can I output alternate values from a set of values-list?
A:

1. Just loop through your values-list array:
<?php
$values_arr = array('A', 'B', 'C', 'D');
for($i=0; $i<10; ++$i)
{
$value = current($values_arr) and next($values_arr) or
reset($values_arr);
echo $value;
}
?>

2. If you want just to flip-flop between two integers, use XOR logic:
<?php
$flip_num = 1;
$flop_num = 5;
for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flop_num))
echo $n;
?>

The idea may be incorporated to echo a HTML table with alternate row
colors, row styles (using CSS class), etc.

++++++
@todo Better wording. Grammar cleanup. Someone may help.

Jul 17 '05 #7

P: n/a
R. Rajesh Jeba Anbiah wrote:
Q: How can I output alternate values from a set of values-list?
A:
2. If you want just to flip-flop between two integers, use XOR logic:
<?php
$flip_num = 1;
$flop_num = 5;
for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flop_num))
echo $n;
?>


Way more complicated than necessary. Much better:

<?php
$flip_num = 1;
$flop_num = 5;
for($i=0; $i<10; $i++)
echo $i % 2 ? $flip_num : $flop_num;
?>

KISS.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #8

P: n/a
Jerry Stuckle wrote:
R. Rajesh Jeba Anbiah wrote:
Q: How can I output alternate values from a set of values-list?
A:
2. If you want just to flip-flop between two integers, use XOR logic:
<?php
$flip_num = 1;
$flop_num = 5;
for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flop_num))
echo $n;
?>


Way more complicated than necessary. Much better:

<?php
$flip_num = 1;
$flop_num = 5;
for($i=0; $i<10; $i++)
echo $i % 2 ? $flip_num : $flop_num;
?>

KISS.


Not really. Your logic requires an iterator variable $i; but none of
the above logic requires such. So, the above logics can easily be
incorporated while reading DB or using foreach or while loops.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com

Jul 17 '05 #9

P: n/a
R. Rajesh Jeba Anbiah wrote:
Jerry Stuckle wrote:
R. Rajesh Jeba Anbiah wrote:
Q: How can I output alternate values from a set of values-list?
A:
2. If you want just to flip-flop between two integers, use XOR logic:
<?php
$flip_num = 1;
$flop_num = 5;
for($i=0, $n=$flip_num; $i<10; ++$i, $n ^= ($flip_num^$flop_num))
echo $n;
?>


Way more complicated than necessary. Much better:

<?php
$flip_num = 1;
$flop_num = 5;
for($i=0; $i<10; $i++)
echo $i % 2 ? $flip_num : $flop_num;
?>

KISS.

Not really. Your logic requires an iterator variable $i; but none of
the above logic requires such. So, the above logics can easily be
incorporated while reading DB or using foreach or while loops.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com


Rajesh,

Your code also requires and iterator variable $i; additionally it
requires a second variable $n and uses a double xor logic.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.