Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

Search with Google style "Results 1 - 10 of 25 for ...


Reply


6 Posts   1476 Views

Avatar
MarijnKampf

Community Member, 164 Posts

22 January 2010 at 3:45am

I've based my site search on [url=http://doc.silverstripe.org/doku.php?id=tutorial:4-site-search]tutorial4[/url]. 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?

Avatar
suntrop

Community Member, 131 Posts

30 May 2010 at 3:07am

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

Avatar
MarijnKampf

Community Member, 164 Posts

30 May 2010 at 10:56pm

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

Avatar
suntrop

Community Member, 131 Posts

30 May 2010 at 11:44pm

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

Avatar
Ben_W

Community Member, 80 Posts

16 August 2010 at 12:13pm

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'.

Avatar
MarijnKampf

Community Member, 164 Posts

16 August 2010 at 6:58pm

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;
      }
   }