Solved

Building up a query string for sorting purposes dynamically from select elements

Posted on 2011-09-26
3
163 Views
Last Modified: 2012-05-12
Hi,

Having used the jquery tablesorter plugin for pagination and sorting purposes on a site, the client wants to use PHP now. Pagination is not a problem at all  - but I need an efficient way to build the query string for sort purposes when I want to be able to sort the list based on (eg) price, name. I need to be able to potentially add extra select elements (or possibily even check boxes at some stage) so the code needs to be able to handle that.

What I need to avoid is repeated occurences of the parameters in the query string eg
?sort=true&priceOrder=1&nameOrder=0&priceOrder=1
which happens if you use something like window.location.href in the jquery - i would like the string to be converted to in this instance
?sort=true&nameOrder=0&priceOrder=1

Any help much appreciated!

Cheers,

Daisy



0
Comment
Question by:daisydoos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 18

Accepted Solution

by:
Garry Glendown earned 250 total points
ID: 36598477
If all you need is avoid adding multiple instances of a parameter, you could put the parameter value into a associative array and fill the URL from that, e.g.:

$myparms=Array();               // init an empty array

$myparms["parm1"]="val1";
$myparms["parm2"]="val2";
$myparms["parm1"]="val3";       // second assignment for that parameter,
                                // last one counts ;)

$url="http://some.url/search.php?";
foreach ($myparms as $key => $value) {
        $url=$url."&".$key."=".$value;
}

Open in new window

Please note, this code is just a rudimentary proof-of-concept, it requires some cleanup (first "&" should be omitted, though it should work with it), values might need escaping, etc ...

0
 
LVL 12

Assisted Solution

by:zappafan2k2
zappafan2k2 earned 250 total points
ID: 36598935
Daisy,

I'm a bit confused about what exactly you are looking to do.  Are you looking for a javascript or PHP solution?  Where are the variables coming from?  Why would there be duplicates?  DO you have some sample code that isn't working?

If it's a PHP solution, I agree with Garry-G about using an associative array, but personally I would use the http_build_query() function
0
 

Author Comment

by:daisydoos
ID: 36709153
Hi,

Many thanks for getting back to me - really appreciate it. I will get back to you when I've had a chance to look at your comments. Zappafan - the sorting is done by PHP but I need to pass the values to the query string each time I change a select variable - and this needs to be done using JS. I can build the query string - as described in my initial message but I was just trying to remove repeated occurences of parameters (eg nameOrder) as you build the url up. I know it doesnt actually matter when you are using $_GET, but it's just a case of keeping the urls short and tidy.

Cheers,

Daisy
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question