Jump to content
brdje

Issues with rendering product catalog

Recommended Posts

Greetings,

 

I've created a product database which all use the template product.php and are published on the website.

I'm trying to create an overview table with pagination of all the products, but using $pages->get does not return an object, only the object title.

$products = $pages->get("template=product, limit=10");

This returns 10 strings in the frontend but not the object. When looping over the result set to get product fields, i get the following error:

Notice: Trying to get property 'title' of non-object

 

Because the product database is fairly large, i've added a pagination using the same query and this does return all the pages, but i can't click on the links that the paginator renders.

$results = $pages->find("template=product, limit=10");
					if($results->getTotal() > 10) {
						echo $results->renderPager(array(
							"nextItemLabel" => "Volgende",
							"previousItemLabel" => "Vorige",
							"currentItemClass" => 'active'
						));
					}

image.png.d8426092052466203c55c33d48f89e67.png

I've added the option in the template to allow page numbers. When i navigate to the link manually, i still end up seeing the 10 first products.

 

I've tried changing several settings around, but i'm a bit stuck on how to resolve these issues. Do you have any advice what I need to adjust?

Thanks in advance for your feedback.

 

 

Share this post


Link to post
Share on other sites

See the docs to understand the difference between $pages->get() and $pages->find():
https://processwire.com/api/ref/pages/get/
https://processwire.com/api/ref/pages/find/

You want $pages->find(). And you only want to execute it once and use $results for both the results list and the pagination.

$results = $pages->find("template=product, limit=10");

// Results list
foreach($results as $result) {
	// Output each individual result
	// ...
}

// Pagination
echo $results->renderPager(array(
	'nextItemLabel' => 'Volgende',
	'previousItemLabel' => 'Vorige',
	'currentItemClass' => 'active'
));

 

  • Thanks 1

Share this post


Link to post
Share on other sites

Thanks for the clarification. I did interpret that incorrectly and using find i get all the requested information correctly.

The pagination links still aren't working at the moment, where the issue remains the same. 

Links are present and rendered, but nothing happens when clicking them. Navigating to the url itself, will always render the first page of results wether being on page 1 or 1831 (current last page)

Share this post


Link to post
Share on other sites
1 hour ago, brdje said:

Links are present and rendered, but nothing happens when clicking them. Navigating to the url itself, will always render the first page of results wether being on page 1 or 1831 (current last page)

Enable pagination/page numbers in your template (the overview template) settings.

2019-11-28_13-43.png.5af108d74184d6ad6c3b1bbf7c77ce78.png

Share this post


Link to post
Share on other sites
2 hours ago, wbmnfktr said:

Enable pagination/page numbers in your template (the overview template) settings.

2019-11-28_13-43.png.5af108d74184d6ad6c3b1bbf7c77ce78.png

"Allow Page Numbers" was enabled in the template allready upon creating the template. But the pagination just doesn't work it seems.

image.png.db7b3ce3bd37f9e57e9062dfce328267.png

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ottogal
      Hello all,
      using PW 3.0.148 with the regular site profile for a blog, I got an an empty pagination output when I had a Toggle field in the selector.
      The Toggle Fieldtype was introduced with https://processwire.com/blog/posts/pw-3.0.139/ .
      The selector resulting in empty pagination:
      $posts = $pages->find("parent=blog, sort=-date, limit=10, toggle_field=0"); It worked well, when I replaced the Toggle field with a Checkbox field:
      $posts = $pages->find("parent=blog, sort=-date, limit=10, checkbox_field=0"); So the prerequisites for the pagination to work are given.
      The settings for the Toggle field were:
      Formatted value: Integer Label Type: Yes/No Input Type: Toggle buttons Default selected option: No Thanks for any hints!
    • By jacmaes
      I have a series of videos, and the following search form (translated into English here) that allows to filter these videos on the frontend:

      I've built a few of these search forms, but only with text fields, selects and radio buttons. Here I'm using an array with checkboxes ("Level" field above), and it's causing me grief when I try to paginate these results. I've done a lot of searching in the forum and spent too many hours to try to get it to work. Here's how I'm building the selector:
      <?php if(count($input->get)): // Level is an array. Code adapted from Ryan's snippet here: // https://processwire.com/talk/topic/3472-enable-pagination-for-search-results/?tab=comments#comment-38042 if($input->get->level) { $level = array(); foreach($input->get->level as $id) $level[] = (int) $id; // sanitize to INTs $level = implode('|', $level); // convert to 123|456|789 string, ready for selector } else { $level = ''; } $data = array( 'training_type' => array('=', (int) $input->get->training_type), 'duration' => array('=', (int) $input->get->duration), 'level' => array('=', $level), 'limit' => array('=', (int) $input->get->limit) ); $selector = ''; // iterate through the $data we made above to create a selector string foreach($data as $field => $a) { list($operator, $value) = $a; if(empty($value)) continue; // send value to the whitelist so that it can be used in pagination $input->whitelist($field, $value); // append to our selector string $selector .= "$field$operator$value, "; } $videos = $page->children("$selector"); When I hit search, I get the expected results. So far so good. The GET parameters are the following with the options selected in the screenshot above:
      videos/?level[]=1476&level[]=1477&training_type=1473&duration=1485&limit=10 $selector echoes the following as the "level" field is an array with a pipe character:
      level=1476|1477, training_type=1473, duration=1485, limit=10 Now, when paginating these results, the following page (page 2) shows these GET parameters:
      videos/page2/?level=1476|1477&training_type=1473&duration=1486&limit=10 And I think that's where the problem lies. The "level" field is "lost" and I'm getting more results than expected on subsequent pages. If I manually add "page2" to the initial results in the URL, just to test, everything works fine:
      videos/page2/?level[]=1476&level[]=1477&training_type=1473&duration=1486&limit=10 But how can I achieve this in code? Do I need to revert to "level[]=1475&level[]=1477" instead of "level=1476|1477" for the pagination to work correctly, and can you PHP gods illuminate me?
      Any help would be really appreciated, really.
    • By Greg Lumley
      Hi! In busy learning to know PW better I'm looking at existing code.
      I have Bitpoets Editorial Responsive Blog as example. While looking through the code I've just found this line in the blog-head.php 
      <link rel="stylesheet" href="<?= $config->urls->templates ?>assets/css/main.css?ts=<?= time() ?>" /> I hope it's not a stupid question but I've never seen this before...
      main.css?ts=<?= time() ?> I notice the main template is empty but of course renders on the front end.
      Using a timestamp really has me totally confused. Can someone explain it to me please. 
      Thank you! 
      Greg
    • By christophengelmayer
      Hi everyone,
      I'm working on a CLI script that renders paginated pages.
      Therefore I iterate trough paginated pages and set the page number and render the result.
      My problem is, after calling the render function for the first time, the output doesn't change even if I change the page number.
       
      DEMO:
      I'm using a template that renders a pagination of its children:
      <?php echo $page->children("limit=3")->render(); ?> I can view the paginated results in the Browser:
      /page-rendering-pagination/ /page-rendering-pagination/page2 /page-rendering-pagination/page3 ... When trying to render the different pages using the API I always get the first result, even if I change the page Number.
      <?php namespace ProcessWire; include('./index.php'); wire('input')->setPageNum(1); $p = wire('pages')->get('/page-rendering-pagination'); var_dump($p->render()); // renders first three items wire('input')->setPageNum(2); var_dump($p->render()); // also renders the first three items Am I missing something? Is there some kind of caching mechanism that I'm not aware of?
      Thanks for your help.
    • By dragan
      Quick question: Is it possible to use PW's pagination for other things than PageArrays? i.e. query custom database tables and paginate the results? Has anyone ever tried it?
×
×
  • Create New...