Jump to content

sort after retrieving results?


bwakad
 Share

Recommended Posts

Been looking on internet and in this forum, and could not find any examples. So I am not really sure on how to do this, but suppose I have a selector which returns me some items, depending on returned quantity : with/without pagination.

I want to make a top bar with some links for certain fields like "date ASC / DESC", or "company".

Once clicked on it, it will sort those results.

Now, to use a link value and to GET that value on a page is not a problem. But, is it wise to (again) use $pages->find, or is there another way of doing this? I need to do this from within a function.

In the API I found getSelectors(), sort("property"), and similar. But to be honest, I am not sure how to capture my returned results.

- - - - - - - EDIT - - - - - - -

actually, the pagination part is not required. Let's face it, if I am on page 3, and do another sort, I would like to be on page 1 after the sort.

Link to comment
Share on other sites

You can sort any WireArray with $a->sort("property") as shown in the API cheatsheet

$a = your WireArray (or PageArray)

property you can set to sort the array

But you already have a $pages->find as far i understood, why not modify the selector of that and extend it with the get variable.

This way you can sort on database level which is faster then getting the data from the database first and then sorting it afterwards.

  • Like 2
Link to comment
Share on other sites

was trying to do it before the find() gets in place -

But do you mean

find() with sort is faster,

find(), then store, then sort is slower?

I always thought, the last was less resourceful / better for server?

case '1006': // id of current page
            if ($_GET['title'] == "asc") {
                $by = "title";
            }
            elseif ($_GET['title'] == "desc") {
                $by = "-title";
            }
            elseif ($_GET['id'] == "asc") {
                $by = "id";
            }
            elseif ($_GET['id'] == "desc") {
                $by = "-id";
            }

            else {
                $by = "{$page->name}";
            }
                $selector = "template=child-template,  limit=4, sort={$by}";
            break;

I am not sure what is best now:

A. use the $_GET before I find(),

B. use the $_GET after find(), first store, then use sort.

in case of B, I would not know how to do this after the find() is created.

but in any way, after of before, I guess the pagination will not account for the $_GET in the url.

Link to comment
Share on other sites

Still did not found a solution. I now use this code before I construct my $selects = find(). My $selects is the variable for the find(something). Then after I get results, I thought, when $selects IS declared, this IF statement would run. I do not receive errors, but the sort() is not picked up. I made spaces here between the () for readability:

if ( isset ( $selects ) ) {
        $a->getArray ( $selects );
        if ( $_GET['sort'] ) {
            $a->sort ( $_GET['sort'] );
        } $selects = $a;
        include ( $layout );
    } else {

// continue with normal code

Link to comment
Share on other sites

case '1006': // id of current page
            if ($_GET['title'] == "asc") {
                $by = "title";
            }
            elseif ($_GET['title'] == "desc") {
                $by = "-title";
            }
            elseif ($_GET['id'] == "asc") {
                $by = "id";
            }
            elseif ($_GET['id'] == "desc") {
                $by = "-id";
            }

            else {
                $by = "{$page->name}";
            }
                $selector = "template=child-template,  limit=4, sort={$by}";
            break;

You already have the code i see. Looks like a good solution.

So method A is the right way to do so in this case.

When you want to retreive a subset of data for example 4 pages out of 1000 records in the database i recommend sorting on database level (like you did in your code). This will be faster since the database fields all are indexed. And it saves you from loading the 1000 records in to memory and sorting it afterwards and then limiting.

Because if you would limit it before sorting you never will get the right results.

  • Like 2
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...