Re: loop variable
| From: | dav | Date: | Wed, 07 Nov 2001 08:25:17 +0000 |
| Subject: | Re: loop variable | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-73687@lists.php.net to get a copy of this message | ||
I think the second test is not containing any value because you are already
parsing the array with that for cycle.
1) I would use the count and not sizeof
2) You don't neet the $test so fetch the position, it's a natural array
index
3) U can use the array's function to set back to the first position of the
array the pointer to the first index it will be read. Infact, a the end of
the FOR cycle, the pointer still remains at the end of the array.
Try some cleaner array functions! :)
J
"Michael P. Carel" <mikecarel@team5.teamglac.com> ha scritto nel messaggio
news:019e01c16760$00d895c0$7f01a8c0@teamglac.com...
>
> Hi there,
>
> I have a problem regarding the "for" loop or other types of loop.
> Im using the "for" loop statement to read and declare a variable for an
> array of elements, but my problem is when i used the declared variable
> outside the loop statement it does not give me the correct output. Here's
my
> sample:
>
> <html>
> <head>
> </head>
> <body>
> <?
> // check for $submit
> if (!$submit)
> {
> // and display form
> ?>
> <form action="test2.php" method="GET">
> <input type="checkbox" name="artist[]" value="Bon Jovi">Bon
> Jovi
> <input type="checkbox" name="artist[]"
> value="N'Sync">N'Sync
> <input type="checkbox" name="artist[]"
> value="Boyzone">Boyzone
> <input type="checkbox" name="artist[]" value="Britney
> Spears">Britney
Spears
> <input type="checkbox" name="artist[]" value="Jethro
> Tull">Jethro Tull
> <input type="checkbox" name="artist[]" value="Crosby, Stills &
> Nash">Crosby, Stills & Nash
> <br>
> <input type="submit" name="submit" value="Select">
> </form>
> <?
> }
> // or display the selected artists
> else
> {
> ?>
> <b>And here are your selections:</b>
> <br>
> <?
> // use a "for" loop to read and display array elements
> for($count = 0; $count < sizeof($artist); $count++)
> {
> echo "<i>$artist[$count]</i><br>";
> $test=$artist[$count];
> print("$test"); /*this line gives the proper output */
> }
> /*print("$test"); when i used this line and comment the previous print
line,
> it does not give me a proper out*/
>
> }
> ?>
> </body>
> </html>
>
> I want to used the for loop output all thru out my program.and not just
> within the loop.
> Please help.
>
>
>
>
> Regards,
>
> Michael P. Carel
> Systems Administrator
>
>
> TEAM PACIFIC CORPORATION
> FTI Taguig Metro, Manila. PHIL.
> Tel. Nos. (02) 838-50-05 local 363
> (02) 838-84-38 direct
>