Passing data with URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

Passing data with URL

Post 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!!!
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

arrays uselly have a value in [].e.g. $columns[value];
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

Post 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!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Last edited by twigletmac on Tue Oct 01, 2002 9:04 am, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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).
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

Thanks guys!

Post 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....
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

OK...that makes sense...

Post by Fari »

...had a look at explode and implode and it looks like a cinch! :)
Fari
Forum Commoner
Posts: 42
Joined: Thu Sep 19, 2002 8:41 pm
Location: Timmins - Ontario

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

Post by Fari »

... like a dream! Thanks for the help! Keep it up!
Post Reply