Forum Moderators: coopster

Message Too Old, No Replies

Looping through a list($test, $test1, $test2)

         

fintan

8:57 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Hi I was wondering is it possible to loop through a list function. Generating the varibles along the way.


list($test, $test1, $test2) = split('&',$_SERVER['QUERY_STRING'],count($testing));

So $test is static and $test1, $test2, $test3... is dynamic. I'm trying to pull out the info out of the QUERY_STRING. Where all the varibles have the same name. Thanks

fintan.

StupidScript

9:04 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How is your query string arranged?

?item=no1&item=no2&item=no3

or more like

?item[]=no1&item[]=no2&item[]=no3

If you use the second example-type, then the item[] array has already been created and you can loop through it directly instead of needing to split() the string to create your array.

foreach ($_GET['item'] as $item) {

fintan

9:13 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Thanks for the reply. I wanted to get rid of the first varible. Here it is.


assign=2&setfolder=00018&setfolder=00017&setfolder=00016

So would it be like


$arr = $_GET[setfolder];
foreach ($arr as $value) {
echo "$value<br />\n";
}

I get an error this way.

fintan

9:35 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



If I do it like that wont it just appear as if its one varible?

fintan

9:50 pm on Mar 31, 2005 (gmt 0)

10+ Year Member




$arr = split('&',$_SERVER['QUERY_STRING'],count($test));
foreach ($arr as $key => $value) {
if($key!= 0){echo $value.'<br />';}
}

Came up with this. Thanks for pointing me in right direction.

StupidScript

10:58 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good job!

Note that if you set up your form (or whatever you are getting the parameters of the URI from) so that the names of the form fields include an empty array bracket, they will naturally form an array, and be separate distinct variables.

You used:

setfolder=

Coming from:
<input name="setfolder">

Or from:
<select name="setfolder" multiple>

You may be able to use:

setfolder[]=

Coming from:
<input name="setfolder[]">

Or from:
<select name="setfolder[]" multiple>

You seem to have a handle on it, but I thought I'd point out this little convention that makes it a bit easier. Your solution is nice, though. :)

fintan

11:17 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Thanks for that just added it. I'm off to bed see ya!