Jump to content

Change sort by "value DESC" to "sort=-value"


kongondo
 Share

Recommended Posts

Hi,

I have a situation where a sort value is being sent by GET to be processed by another PHP file in PW. The value is being sent via AJAX. The script I am using uses "value ASC" or "value DESC" as sorting parameters. For instance, the sort value could be "name DESC", (sort by name descending). The value is passed to a PW find() to grab some results and sort them according to the sent sort value. Obviously the following will not work with PW selectors:

$pages->find("template=basic-page, limit=10, sort=name DESC");// sort will not work

How can I convert the sort value coming in via AJAX ("name DESC") (without hacking the core of that script) to achieve "-name" for use in PW selectors as follows?


$pages->find("template=basic-page, limit=10, sort=-name");

Thanks.

Link to comment
Share on other sites

If you have few variations of sort you could do it with a switch statement:

switch ($get) {
    case "name DESC":
        $sort = -name;
        break;
    case "name ASC":
        $sort = -name;
        break;
    // and so on...
}
 
If there are more variations you can automate it like this:
$sort = explode(" ", $get);

$order = $sort[1] == "DESC" ? "-" : "" ;

$value = $sort[0];

$pages->find("template=basic-page, limit=10, sort=$order$value");
  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...