PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Nov 24, 2017 5:24 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Thu Sep 11, 2003 8:36 am 
Offline
Forum Newbie

Joined: Thu Sep 11, 2003 3:32 am
Posts: 11
OK, I've had no joy with this in various 'normal' forums - so I'm moving up to advanced ...

Passing Arrays in URLs. I.e passing arrays between two pages.

The generally accepted options (which I'm sure you know) are:

serialize and urlencode .... then ... urldecode and unserialize

OR

implode ... then ... explode

OR

use session variables/cookies

But here is my argument as to why I think there might be a way that lets PHP automatically do the unencoding part:

If you create a form with a multiple select in it like so:

<SELECT NAME=selectname[]> etc

.. the array $selectname appears automatically in the backend when the form is submitted. Note that $selectname is an array. This means that php must automatically create arrays from data POSTed in a particular format.

If we could pass an array in this format in a url or a hidden form then we wouldn't have to do anything on the recieving page.

Is this argument correct and does anyone know what this format is?

It would certainly simplify the handling of forms with multiple select boxes in them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 8:43 am 
Offline
Admin
User avatar

Joined: Wed Aug 13, 2003 7:02 am
Posts: 4522
Location: York, UK
But what if you aren't submitting a form?

and the

Syntax: [ Download ] [ Hide ]
&lt;SELECT NAME=selectname&#1111;]&gt;


solution is already well known...to me anyway

Mark


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 10:57 am 
Offline
Forum Newbie

Joined: Thu Sep 11, 2003 3:32 am
Posts: 11
No, no - I'm not proposing the multiselect as a way of passing parameters I'm saying that it indicates that there is another way of passing arrays (apart from the ones in my first post) - without actually having a multiselect.

I'm saying that when you submit a form with a multi select, the form is passed to the server via POST with the array from the form in some format. Now we know that this POSTED array automatically appears to PHP scripts in the form of an array. This means that the php engine must see the posted variable in some format and AUTOMATICALLY convert them to an array. If we could replicate this format when calling the page from a link or via hidden elements and javascript then we could pass arrays without having to recreate them on the receiving page.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 11:12 am 
Offline
Admin
User avatar

Joined: Wed Aug 13, 2003 7:02 am
Posts: 4522
Location: York, UK
you mean like this?

save this code as test.php and run.

Syntax: [ Download ] [ Hide ]
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;a href="test.php?checkbox&#1111;]=checkbox1&amp;checkbox&#1111;]=checkbox2&amp;checkbox&#1111;]=checkbox3"&gt;Click Me&lt;/a&gt;
&lt;?

        print_r($_GET&#1111;'checkbox']);

?&gt;

&lt;/body&gt;
&lt;/html&gt;


Mark


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 11:40 am 
Offline
Forum Regular
User avatar

Joined: Thu Jan 23, 2003 10:45 pm
Posts: 782
Location: SWNY
naming a form element with [] is not another way to transmit arrays, you can always explicit name something with full array index or associative key

<input type="text" name="form[firstname]">
<input type="text" name="form[lastname]">

The only difference when using [] is PHP's native handling of $var[] = 'something' that id adds a new item and picks the next numeric index for it.. so if you gave every element in your form the same name, PHP would just index/number them all..
Nothing new, nothing exiting really..

Not sure if I understood what the original "Problem" is..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 12:48 pm 
Offline
Forum Commoner
User avatar

Joined: Tue Oct 01, 2002 5:28 am
Posts: 89
Location: Gothenburg Sweden
I think that what alexp is trying to do is passing arrays between webpages without using forms... correct me if I'm wrong...

I don't know of a direct solution but is keen on finding out....

//cybaf


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 2:21 pm 
Offline
Forum Newbie

Joined: Thu Sep 11, 2003 3:32 am
Posts: 11
Which does mean I can pass a numeric key array like so:

print "<A HREF=\"form.php?".implode("&array_name[]=",$array_name)."\">";

and I wouldn't have to do anything at the other end to reconstruct it - php would create an array called $array_name.

This is useful because you can link to a form with a multi-select in it, pass the initial values of the multi-select in the link and then use the same function/page for subsequent calls to the form page (if they fill something in wrong, for instance) and that function will always be passed an array. Neat. Otherwise, you'd have to pass in the initial values as a string. Then you'd have to test at the start of the function to see if the multi select variable was an array or not. If it wasn't you'd have to explode the string.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 2:48 pm 
Offline
Forum Regular
User avatar

Joined: Thu Jan 23, 2003 10:45 pm
Posts: 782
Location: SWNY
Yes that may work (Does [ and ] need to be urlencoded perhaps?), the values however MUST be urlencoded unless they are very plain.. but this is only useful if all you need is a bunch of strings or numbers and what index number they have does not matter, and none of the values is an array.
Syntax: [ Download ] [ Hide ]
Example:  <?php

 

  $test = array (

     'a',

     'a' => 'b',

    'beer' => 'Sam Adams',

    31 => 'My Age',

    14 => 3,

     4 => 99,

     'stuff' => array ('hello','boette')

  );



  $collapsed = implode ("\n", $test);

  var_dump ($collapsed);



?>

will result in this:



string(31) "a

b

Sam Adams

My Age

3

99

Array"



while it was



array(7) {

  [0]=>

  string(1) "a"

  ["a"]=>

  string(1) "b"

  ["beer"]=>

  string(9) "Sam Adams"

  [31]=>

  string(6) "My Age"

  [14]=>

  int(3)

  [4]=>

  int(99)

  ["stuff"]=>

  array(2) {

    [0]=>

    string(5) "hello"

    [1]=>

    string(6) "boette"

  }

}





And when trying to uncollapse it with

<?php

  $test = explode ("\n",$collapsed);

  var_dump ($test);

?>



you get



array(7) {

  [0]=>

  string(1) "a"

  [1]=>

  string(1) "b"

  [2]=>

  string(9) "Sam Adams"

  [3]=>

  string(6) "My Age"

  [4]=>

  string(1) "3"

  [5]=>

  string(2) "99"

  [6]=>

  string(5) "Array"

}

so as you can see, all type definitions, keys and indexnumbers are lost, and arrays will not work...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 3:17 pm 
Offline
Forum Newbie

Joined: Thu Sep 11, 2003 3:32 am
Posts: 11
Wow - you lot are real glass half-empty kinda guys!

In the fairly common situation I was intending it for - explained above - you'll have control over what the array contains and in all but very obtuse cases you'll want it to be only letters or numbers. In that situation it will also not be an array of arrays. And it leads to much neater code.

In other cases where you have control over the contents I think it's a lot neater than the alternatives.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 3:55 pm 
Offline
Site Admin

Joined: Thu Apr 18, 2002 3:14 pm
Posts: 1767
Location: Montreal, CA
I'm still trying to figure out why you aren't using Sessions for this. That is why you have sessions.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 7:18 pm 
Offline
Forum Newbie

Joined: Thu Sep 11, 2003 3:32 am
Posts: 11
Well, in the example I give, the variable is only used briefly on (essentially) one page so (to prevent session variable runaway) you'd have to register and unregister the session variable and you'd also have to get the multi-select results in and out of the variable every time the page was accessed (which would generally happen consecutively anyway). This way after that initial construction you don't actually have to write any more code.

I don't get why you guys are so pessimistic about this - in the situation I describe alone it seems to me that it is a neat trick and fairly obviously the most efficent (code wise at least) way of dealing with it. And it's really quite a common situation.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 7:53 pm 
Offline
DevNet Master

Joined: Thu Jan 30, 2003 9:26 pm
Posts: 2893
Location: Glasgow, Scotland
It's good to try to think up new ways of doing things - that's an important skill.

However, there's a world of experience behind the "pessimism": custom methods which only work in limited circumstances aren't very useful. I think good design pushes in the other direction, towards greater abstraction and greater flexibility.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 2:23 am 
Offline
Her Royal Site Adminness
User avatar

Joined: Tue Apr 23, 2002 2:21 am
Posts: 5371
Location: Essex, UK
Please don't cross-post, if you feel a post should be moved ask for it to be moved. Don't just start the topic again in another forum.

Mac


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 3:16 am 
Offline
Forum Newbie

Joined: Thu Sep 11, 2003 3:32 am
Posts: 11
McGruff,

That's simply not true. The best code comes from finding the best method for the task. PHP itself is a large group of custom methods - if you want the greatest abstraction you program in machine code or assembly language.

And anyway, I don't think the need to pass single dimensional arrays with numerical keys is a limited circumstance - there was a time when all programs were written with arrays of that sort. And as I keep saying, even the one example I give is quite common.

Many appolgies to Mac.

alexp


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 7:39 am 
Offline
Forum Regular
User avatar

Joined: Thu Jan 23, 2003 10:45 pm
Posts: 782
Location: SWNY
The _very_ limited circumstance is caused by the fact that the values of the array can not contain spaces nor any other character that needs url-encoding....


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 24 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group