Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » Search with Google style "Results 1 - 10 of 25 for ...

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1358 Views
  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Search with Google style "Results 1 - 10 of 25 for ... Link to this post

    I've based my site search on tutorial4. I would like to extend this with a line at the top saying:

    Results 1 - 10 of 25 for 'query'.

    However, I can't seem to make it work correctly. I can get the pageStart, but this starts at 0 rather than 1. I tried adding the pageLength to the pageStart but I couldn't do calculations in a templates.

    I've tried adding the data to the Page_Controller

       function results($data, $form){
           $data = array(
           'Results' => $form->getResults(),
           'Query' => $form->getSearchQuery(),
        'Title' => 'Search Results',
        'PageStart' => $form->getResults()->pageStart+1,
        'PageEnd' => $form->getResults()->pageStart + $form->pageLength,
           );

           return $this->customise($data)->renderWith(array('Page_results', 'Page'));
       }


    but this didn't work as the pageStart didn't change from 0.

    I've tried the following in the template:

    Results <strong>$Results.pageStart - $Results.pageEnd</strong> of <strong>$Results.TotalItems</strong> for <strong>$Query</strong>

    but this didn't work either.

    Does anyone know of a way to make this work?

  • suntrop
    Avatar
    Community Member
    131 Posts

    Re: Search with Google style "Results 1 - 10 of 25 for ... Link to this post

    Marijn, did you find a solution? I need these numbers as well

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Search with Google style "Results 1 - 10 of 25 for ... Link to this post

    No luck so far, sorry. Please let me know if you find a solution.

  • suntrop
    Avatar
    Community Member
    131 Posts

    Re: Search with Google style "Results 1 - 10 of 25 for ... Link to this post

    I came across with this code
    There are <strong>$Results.TotalItems results</strong> (Page $Results.CurrentPage of $Results.TotalPages)

    That doesn't give you 1-10, 11-20… but maybe you can work with CurrentPage. There is a variable for the current result set that says how many results on this page are. Can't remember what is like but it is in the Docs of SS.

    Bey
    suntrop

  • Ben_W
    Avatar
    Community Member
    80 Posts

    Re: Search with Google style "Results 1 - 10 of 25 for ... Link to this post

    Have a look at DataObjectSet.php in /sapphire/core/model/, around line 320, it should have some build in function you may use in your template. Total number of search result page, try 'TotalPages'.

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: Search with Google style "Results 1 - 10 of 25 for ... Link to this post

    I forgot I posted this thread. I ended up calling the setPageLimits function.

                $Results->setPageLimits($start, Aspect::$pageSizeResults, $rowCount);
                $limits = $Results->getPageLimits();

    In your code template you can use:

           <div class="nrResults">Found $Results.totalSize results</div>
           <div class="pages">Page $Results.CurrentPage of $Results.TotalPages pages</div>

    Full php function (please note code may not run direct as I've altered it from a custom dataobject)

       function ResultsPaged() {
          if (!$this->HideResults) {
             if ($this->tempMetaTitle == null) $this->tempMetaTitle = $this->owner->MetaTitle;
             if(!isset($_GET['page']) || !is_numeric($_GET['page']) || (int)$_GET['page'] < 1) {
                $start = $_GET['page'] = 0;
             } else {
                $page = $_GET['page'];
                $start = ($page - 1) * Aspect::$pageSizeResults;
             }

             $query = new SQLQuery(
                $select = "`Page`.*",
                $from = array("Page"),
                $where = "",
                $orderby = "",
                $groupby = "",
                $having = "",
                $limit = "$start, " . Aspect::$pageSizeResults
             );
             $query->distinct = true;

             $result = $query->execute();
             $Results = singleton('Page')->buildDataObjectSet($result);
             $rowCount = $query->unlimitedRowCount("DISTINCT Name");

             if($Results) {
                $Results->setPageLimits($start, Aspect::$pageSizeResults, $rowCount);
                $limits = $Results->getPageLimits();
                if (!isset($page)) $page = 1;
                if ($limits['totalSize'] > $limits['pageLength']) {
                   $this->owner->MetaTitle = "Page " . $page . " of " . $Results->TotalPages() . ": " . $this->tempMetaTitle . " - " . $limits['totalSize'] . " results found";
                } else {
                   $this->owner->MetaTitle = $this->owner->MetaTitle . " - " . $limits['totalSize'] . " results found";
                }
             }

             return $Results;
          }
       }

    1358 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.