472,146 Members | 1,260 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

How can I change every second row?

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
9 8801
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
Thanks a lot, I totally cool.

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

but perhaps there is another way?

Jul 17 '05 #3
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
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
>>> 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
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
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
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
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.

Similar topics

27 posts views Thread by StevePBurgess | last post: by
5 posts views Thread by mabond | last post: by
25 posts views Thread by =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post: by
25 posts views Thread by Peng Yu | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.