Arrays (?) and URLs
Posted by Caitlinn (Caitlinn), 9 September 2004Okay, this will be a dozy for me to explain because -- whilst I can make this work -- I don't know the proper terminology. So please bear with me ...
I have a form which contains a dropdown box (list of categories) and 51 checkboxes (one for each US state, plus DC). The object of the form is to search the database for all companies that match the selected category and state(s).
The checkboxes on the form are written as:
The information is then passed to the processing script via $_GET (I tried the same thing with $_POST, but the script kept losing the set variables as you paged through results). I have no problem selecting information; it works like a charm. I start running into problems with pagination and the $_GET results. I suppose it's not really a problem, but more of an untidy look to the URL as result of the passed $_GET variables.
The URL looks like:
on the first page and on the second, like:
Surely there must be a neater URL scheme than this. Any pointers in the right direction, please?
Posted by admin (Graham Ellis), 9 September 2004Hmmm ... I'm half with this / half reading this during a course. I think you're maintaining state with your GET method via the URL and when you use the POST method it's getting lost, as the data isn't in the URL any longer. I would be tempted to use a session but that's a whole new can o'worms.
Please post a follow up if you would like me to have a further look overnight tonight - quite happy to do so once the course is over. But I have to teach Perl Objects in 10 minutes
Posted by Caitlinn (Caitlinn), 9 September 2004Yes, please Graham, any further help with this would be much appreciated. In the meantime, I will take your advice and look at the sesssions method.
Thanks a bunch.
I should also mention that pagination works off of $_GET too. I've been fiddling with registering state_id and category variables as $_SESSION variables, but haven't had much luck so far; still losing the state_id and category variables as I paginate through the pages.
I did something like:
Then used the $SESSION['foo'] throughout the rest of my code, as needed.
Posted by admin (Graham Ellis), 10 September 2004OK - A fresh morning. Let's see if I've got this ....
You've filling in a form and getting results back and it's working like a charm ... but you need to move on to another set of results based on the original input, thus you need to carry on your original inputs through to the second result set.
The "conventional" way to do this is to add an extra query string onto the end of the URL and mimic the GET method of form submission - it sounds like it's more or less what you're doing already and it works fine, but it does give what we call the "Yuk" factor of horrid URLs. Don't be too worried about this; you'll see exactly the same sort of thing happen if you use the search engines and go on to subsequent result pages (and you'll also see it at the top of pages on a forum like this one!)
The first alternative way of passing information on is via a POST method. But this means that you have to supply a form - at the least a submit button with hidden fields - rather than a simple "a href" with the data encoded, and it means that your users can't bookmark that second and subsequent page either.
The second alternative is to use a cookie so that your visitor can be remembered. This has great attractions if you user is likely to be going through a whole series of pages, or is likely to be coming back and carrying on in an hour or two, or a day or two. The PHP built in session functions will work like a charm and save you the need to write all your own cookie handling and cart code.
I *hope* I'm talking in the right direction here - I started off saying a "fresh" new morning but I'm not entirely fresh yet, even though it's a new morning!
Posted by Caitlinn (Caitlinn), 10 September 2004on 09/10/04 at 05:00:40, Graham Ellis wrote:
Yes, that's right.
Query strings, on the whole, don't really bother me that much. The Yuck factor came into play with the host of square brackets (the more states you have, the more square brackets you get -- and conceivably, someone could search all 50 states). It's the first time I'd seen them used in query strings, so I was a little worried that I might be doing something a bit 'rigged'.
Okay, this intrigues me because I'm not worried about bookmarking. If using hidden fields on the form, how do you get the $state_id array to explode in the hidden field, so that when it is passed to the next page, it's ready to use? And if you are using the POST method, don't you run the risk of losing your variables as you page through results, unless those variables are in the URL or registered as a part of a session?
I imagine I could take another look at sessions -- I probably did something wrong with my first effort. But here's a question: can you use data supplied in GET in combination with data supplied in SESSION variables?
Thanks again, Graham.
Posted by admin (Graham Ellis), 11 September 2004on 09/10/04 at 09:10:18, Caitlinn wrote:
The "principle" is that you can carry information from one page to the next via the URL ("get"), or via hidden fields (via post or get methods) or via a cookie. Then you have a secondary choice in that you can include all the information your want in the get/post/cookie or you can include just a unique key value which is the name of a disc file in a temporary area or a record in a database which in turn contains the information you want to pass on.
Sound a bit complicated? It isn't - but - it can be a bit long winded to write / understand the first time, and you also need to have a mechanism to go back and clean up all the abandoned sessions unless you want you disc to start getting cluttered ... which is why the session functions and $_SESSION were introduced into PHP.
Yes - both are available to you and if you want to retain something, simply copy it from $_GET to $_SESSION.
There's an example of a 4 stage process using sessions on our website at http://www.wellho.net/demo/sc6.php4 and you can see the source code of this from the training module description page at http://www.wellho.net/resources/H115.html. Basically, this is a great framework for any multipage application
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: email@example.com • WEB: http://www.wellho.net • SKYPE: wellho