Page 1 of 1

How to cycle sortby?

Posted: 30 Sep 2017 09:08
by pleiades
Hello everyone,

I need a little help with what I'm trying to make, I like to cycle sort similar to the cycle through views on another thread.

I changed the cycle through view script to the sortby and sort but its not working.

Code: Select all

$mode = get("Sort");
    if ($mode == #321) {sortby "Created","a";}	
    elseif ($mode == #325) {sortby "Name","a";}
The script is trying to cycle between sort by created and sort by name.

Re: How to cycle sortby?

Posted: 30 Sep 2017 11:03
by bdeshi
get('sort') returns a string formatted as: "SortColumn[,Order]", such as

Code: Select all

Name,a
Custom 2,a
Can this be == to "#321" or any string like this? You need to compare a similar string.

Code: Select all

$sort = get("Sort");
    if ($sort == "Name,a") {sortby "Created","a";}   
    elseif ($sort == "Created,a") {sortby "Name","a";}
I'm just pointing you at the right direction. However, this will not work if sort is descending, even if sorted by Created or Name, because then $sort will end with ",d" instead of ",a", and the comparison will fail. You can use gettoken($sort, 1, ',') to get the sorted column, and gettoken($sort, 2, ',') to get the sort order. Then construct necessary comparisons using these values.
Hope this helps.

Re: How to cycle sortby?

Posted: 30 Sep 2017 11:24
by pleiades
Thank you SammaySarkar for your guidance, :tup: :tup: I was able to make it work, sort of. here is the code if anyone needs it.

The code cycles between Name and Created Column, if you want other column just add more case statement.

Code: Select all

	$sort = get("Sort");
 switch ($sort) {
   case "Name,a":
       sortby "Created","a";
       break;
   case "Name,d":
       sortby "Created","d";
       break;
   case "Created,a":
		sortby "Name","a";
		break;
   case "Created,d":
		sortby "Name","d";
		break;