Jump to content

Using FieldTypeCache with Pagination


Alex CA
 Share

Recommended Posts

I am currently designing a search page for my website.

While I am able to get both pagination and fieldtype caching enabled separately, I am not able to implement them together.

I followed the following 2 pages - 

  1. https://processwire.com/api/modules/markup-pager-nav/
  2. https://processwire-recipes.com/recipes/set-up-search-index-with-fieldtypecache/

Is there any way where I can combine these 2 methods for my website?

 

Here is a snippet of my code

 

$q = $sanitizer->text($input->get->q);

$q = $sanitizer->selectorValue($q);

    // Search the cacged fields for our query text.
    // Limit the results to 50 pages.
    $selector = "search_cache~=$q, limit=50";

    // If user has access to admin pages, lets exclude them from the search results.
      if($user->isLoggedin()) $selector .= ", has_parent!=2";

    // Find pages that match the selector
          
    $matches = $pages->find($selector);
    
    //Get range of paginated results
      $start = $matches->getStart() + 1;
      $end = $matches->getStart() + count($matches);
      $total = $matches->getTotal();
         
    $content = "<p><b>Showing search results ". $start . " - ". $end . " of ".$total."</b></p>";
         
    // did we find any matches?
    if($matches->count) {
      
        $content .= '<ul>';
        foreach($matches as $match){
            $content .= '<li>'. $match->parent->title . ': <a href="' . $match->url . '">' . $match->title . '</a></li>';
        
        }
        $content .= '</ul>';
        
        $content .= $matches->renderPager(array(
            'listMarkup' => "<ul class='pagination'>{out}</ul>",
            'nextItemLabel' => "Next",
            'previousItemLabel' => "Previous",
  
        ));
        
    } else {
        // we didn't find any
        $content = "<strong>Sorry, no results were found.</<strong>";
    }

 

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

×
×
  • Create New...