Page 1 of 1

Passing data with URL

Posted: Mon Sep 30, 2002 4:12 pm
by Fari
Hi

the code below passes the first two parameters correctly to open_db.php, but the third one, an array shows up with a content of the letters 'a r r a y' in the first 5 positions of the array and the rest is empty!!

Code: Select all

<A HREF="open_db.php?tabname=<?=$tabn?>&cols=<?=$coln?>&colmns=<?=$columns?>" ALT="All Honki Dori">Yes</A>
the URL window looks like this on calling open_db.php:

Code: Select all

http://pcloaner3-2k/test/open_db.php?tabname=Test&cols=2&colmns=Array
any ideas why??? Please!!!

Posted: Mon Sep 30, 2002 4:36 pm
by qads
arrays uselly have a value in [].e.g. $columns[value];

Posted: Tue Oct 01, 2002 1:52 am
by twigletmac
You can't just echo an array. As qads pointed out you need to reference the element(s) you want to display. To see what those elements are you could do:

Code: Select all

echo '&lt;pre&gt;';
print_r($columns);
echo '&lt;/pre&gt;';
Mac

Posted: Tue Oct 01, 2002 8:54 am
by Fari
Guys - I'm fully aware of how to diplay the elements of the array. The problem is that if I do this in open_db.php (assuming 10 elemnts were initialized in the script calling open_db.php):

Code: Select all

for ( $i=1; $i<10; $i++ ) echo"colmns&#1111;i]";
the resulting output is: a r r a y and I get an error message of uninitilized element for indices 6,7,8,9,10!!! So instead of the original 10 values passed through colmns=$columns in the URL the characters a r r a y are passed! having said that I can pass an array without problem with POST forinstance. It just doesn't make sense!

Posted: Tue Oct 01, 2002 9:04 am
by twigletmac
When you try and put the array in the query string you are in effect doing

Code: Select all

echo $columns;
which returns 'Array', then when you try and access $columns on the next page $columns is not an array, it is a string which contains the 5 characters a r r a y. So you can no longer access the array elements because there aren't any. You could serialize() or implode() the data, or if there's lots of it, pass it via sessions instead.

Mac

Posted: Tue Oct 01, 2002 9:04 am
by nielsene
You can't pass an array directly as a get/post parameter. You can pass an array as ?value[1]=stuff&value[2]=moresturee.... but you can't pass the whole array because the resulting url is as you wrote ?variable=array.

Sessions can handle arrays natively so you might want to see if it makes sense to pass the values you need as a session variable. Otherwise you're going to have to pass each field individually.

Another option would be to use some packing/unpacking scheme, such as changing the array to a textual, fixed deliminator string that can be passed as a single value (implode/explode).

Thanks guys!

Posted: Tue Oct 01, 2002 9:56 am
by Fari
Will give it a bash! Although the spooky thing is that with POST I can pass the array, if the elements are intialized in that FORM....

Posted: Tue Oct 01, 2002 9:58 am
by nielsene
POST doesn't really send array, it just appears to, it does the whole
variablename[]=foo&variablename[]=bar.... and php knows how to assemble this back into an array.

OK...that makes sense...

Posted: Tue Oct 01, 2002 10:39 am
by Fari
...had a look at explode and implode and it looks like a cinch! :)

yeeeharghhh!! Thanks guys! imlode/explode ....

Posted: Tue Oct 01, 2002 3:44 pm
by Fari
... like a dream! Thanks for the help! Keep it up!