Towards a better way for Arrays in URLs

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

alexp
Forum Newbie
Posts: 11
Joined: Thu Sep 11, 2003 3:32 am

Towards a better way for Arrays in URLs

Post by alexp »

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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

But what if you aren't submitting a form?

and the

Code: Select all

<SELECT NAME=selectname&#1111;]>
solution is already well known...to me anyway

Mark
alexp
Forum Newbie
Posts: 11
Joined: Thu Sep 11, 2003 3:32 am

Post by alexp »

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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you mean like this?

save this code as test.php and run.

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

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

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

?>

</body>
</html>
Mark
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

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..
User avatar
cybaf
Forum Commoner
Posts: 89
Joined: Tue Oct 01, 2002 5:28 am
Location: Gothenburg Sweden

Post by cybaf »

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
alexp
Forum Newbie
Posts: 11
Joined: Thu Sep 11, 2003 3:32 am

Post by alexp »

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.
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

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.

Code: Select all

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...
alexp
Forum Newbie
Posts: 11
Joined: Thu Sep 11, 2003 3:32 am

Post by alexp »

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.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

I'm still trying to figure out why you aren't using Sessions for this. That is why you have sessions.
alexp
Forum Newbie
Posts: 11
Joined: Thu Sep 11, 2003 3:32 am

Post by alexp »

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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

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

Post by twigletmac »

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
alexp
Forum Newbie
Posts: 11
Joined: Thu Sep 11, 2003 3:32 am

Post by alexp »

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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

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....
Post Reply