Forum Moderators: coopster

Message Too Old, No Replies

how to count total iteration for 2 loops?

newbie question

         

macdar

6:22 pm on May 12, 2005 (gmt 0)

10+ Year Member



Hi,
I got stuck trying to count total iterations for two loops. My code is as follows:

for($i=1; $i<10; $i++){
echo $i;
for($j=1; $j<5; $j++){
echo $j;
}
}
it's gonna produce results as follows:
1 - 1,2,3,4
2 - 1,2,3,4
etc..

How to make this script output:
1,2,3,4,5,6,7..35,36?

Thanks.

crashomon

7:09 pm on May 12, 2005 (gmt 0)

10+ Year Member



please clarify, your question doesn't make sense. Are you trying to add the two loops together and have them display as one line? or are you trying to show the sum of the two loops?

macdar

7:25 pm on May 12, 2005 (gmt 0)

10+ Year Member



I'm sorry about that. I tried to simplify that example as much as I could. I've got some sql queries implemented in that script. What I need to do is to get the sum of these iterations step by step.

for($i=1; $i<10; $i++){
echo $i;
for($j=1; $j<5; $j++){
//echo $j;
$q = "update table set wizard= '$wizard' where id = '$k';
}
}

where $k is 1,2,3,..36 (4*9).

Does it make sense now?

Thanks.

py9jmas

7:46 pm on May 12, 2005 (gmt 0)

10+ Year Member



Is there any reason why
for($i=1; $i<37; $i++){
echo $i;
$q = "update table set wizard= '$wizard' where id = '$i';
}

wouldn't work?

macdar

7:56 pm on May 12, 2005 (gmt 0)

10+ Year Member



Yes, that would be to easy..:)

The first loop takes results from DB..

the second one bases on a reglar expression function:

preg_match_all($what, $where, $search_results);

for($i = 0; $i < count($search_results[1]); $i++)
{
....
}

any ideas?

ergophobe

8:28 pm on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$count = 0;
for($i=1; $i<10; $i++)
{
for($j=1; $j<5; $j++)
{
$count++;
echo $count;
}
}

That should do it.

macdar

8:37 pm on May 12, 2005 (gmt 0)

10+ Year Member



that's exactly what I meant, thanks ergophobe!

ergophobe

9:01 pm on May 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You're welcome. Mostly a matter of getting you to the right forum ;-)

[This thread was moved here from The WebmasterWorld Community Center]