Passing variables in multilevels lists
This questions is about lists of data in a back-office, with variables like order, # of page, filter, etc..
I used to pass this variables between pages by GET, but I had many troubles and changed to storing this variables in session. I don't mind bookmarking problem, since it's a back-office tool. All was ok until today: I was viewing a list, I changed one filter, and then I wanted to go back to previous results. My natural gesture was pressing browser "back" button. Of course, I wasn't taken to previous results, because I had overwritten the session.
So, which is the best method to do this? Having in mind not only user's experience, but coding facilities and avoiding mistakes.
Troubles I've found when I passed variables by URL, specially in two or three levels lists:
- There are many variables and many places where I have to put them: link to editting an item, form's action, back links, page navigation links...
- Some variables can have the same name (i.e., each level will have a number of page). It's easy to make a mistake with this, specially when you have to code new things inside a previous script.
- I can't just use the query string, because there are cases in which I have to change some var's value (i.e., in paging navigation links I've to change number_of_pag variable).
I suppose the best solution is not "Hey, if you pay more attention, you won't make th开发者_StackOverflow社区ese mistakes". Is there any system I haven't discovered yet? How the great ones do this?
Maybe implementing methods to render links automatically?
Thanks.
The place for these types of variables are the query string. Not only is it needed for bookmarking, but it is needed for navigating to the given content in general, as you discovered with the back button. However, if you really want to abbreviate the query string, consider using a token or identifier instead.
That is, in session, consider using a system like this:
// this represents the query data for one page request
$_SESSION['queryid']['38hghfd85423lk'] = array(
'all' => array('the => 'query', 'data'),
'is' => array('stored', 'like', 'this)
);
// this represents the query data for another page request
$_SESSION['queryid']['432423jkgrfsd9'] = array(
// .... other data ....
);
Now your query string will look like so:
http://www.example.com/index.php?queryid=38hghfd85423lk
Or like this:
http://www.example.com/index.php?queryid=432423jkgrfsd9
Of course, you use the queryid given in in the GET data to identify which query data to use from $_SESSION
. Now the browser back button will work.
A problem may be that, after long sessions, the cache of queries in $_SESSION
will become quite large. To mitigate this you can limit the length of the cache, say to the last 100 pages. If a user tries to navigate more than 100 pages back, you will notice their queryid no longer exists and will be able to notify them that the page expired.
If you wanted to support bookmarking, you can save the queryids and data to a database. Of course, you may still have to consider pruning methods -- say, queryids that haven't been used in a month are cleared. This may be frustrating to a user, however.
精彩评论