Jump to content

Recommended Posts

Hi guys, have been having an issue with pagination. It's a bit odd,

I have it working fine as per Ryan's docs but when I get to a page2 of results, my categories don't display. I should add that my categories are generated at the top of the page in their own loop.

The template also uses urlSegment to filter the results by category. I have added pagination to both the news template and also the categories template which get's called through urlSegment.

On the main news page, pagination works but the categories come up empty when on the 2nd page.

On the categories template, the pagination won't show at all, and I'm not sure why as it's pretty much an identical setup to the news template.

Here's all the code, in case any of you can make sense of it.... Thanks.

News index :

<?php include("./header.inc"); ?>

<div id="news_index" class="grey_gradient">

<?php if ($input->urlSegment1 !="") {
include ("./categories.php");
} else { ?>

<div id="news_top_bar">

<h5 id="news_header">Recent news items</h5>

<?php $news_cats = $pages->find("template=news_category");
if ($news_cats) { ?>

<ul id="news_cats">
<li>By category:  </li>
<?php foreach ($news_cats as $cat) {
$numArticles = $pages->count("template=news_entry, news_category_link=$cat");
if ($numArticles > 0) {
?>
<li><a href="<?php echo $page->url  . $cat->name; ?>"><?php echo $cat->title; ?></a>  /  </li>
<?php } ?>
<?php } ?>
</ul>

<?php } ?>

<div class="clear"></div><!-- /.clear -->

</div><!-- /#news_top_bar -->

<?php $news = $page->children("sort=-created, limit=3");

foreach ($news as $entry) {

$class = "news_box";

if ($entry == $news->last()) { $class .= " news_box_last"; } ?>

<div class="<?php echo $class; ?>">

<div class="news_index_text">

<h3><a href="<?php echo $entry->url; ?>"><?php echo $entry->title; ?></a></h3>
<h6><?php echo $entry->entry_date; ?>  by  <?php echo $entry->createdUser->first_name . " " . $entry->createdUser->last_name; ?></h6>

<?php if ($entry->summary) {

echo "<p>" . $entry->summary . "</p>";

} else {

$body_text = strip_tags($entry->body);
$text = substr($body_text, 0, 300);
echo "<p>$text...</p>";
} ?>

<a href="<?php echo $entry->url; ?>" class="button">Read on</a>

</div><!-- /.news_index_text -->

<?php if ($entry->main_image) {

$news_image = $entry->main_image->size(240,180); ?>

<img src="<?php echo $news_image->url; ?>" width="<?php echo $news_image->width; ?>" height="<?php echo $news_image->height; ?>" alt="<?php echo $entry->title; ?>" class="small_frame" />

<?php } ?>

<div class="clear"></div><!-- /.clear -->

</div><!-- /.news_box -->

<?php }
?>

<?php // PAGINATION LINKS
$pagination = $news->renderPager();
echo $pagination;
?>

<?php } ?>

</div><!-- /#news_index -->

<?php include("./footer.inc"); ?>

Categories.php :

<?php
$name = $sanitizer->pageName($input->urlSegment1);
$category = $pages->find("template=news_category, name=$name");
$news_cats = $pages->find("template=news_category");
?>

<div id="news_top_bar">

<h5 id="news_header">Recent news items - <?php echo ucwords($name); ?></h5>

<?php if ($news_cats) { ?>

<ul id="news_cats">
<li>By category:  </li>
<?php foreach ($news_cats as $cat) {
$numArticles = $pages->count("template=news_entry, news_category_link=$cat");
if ($numArticles > 0) {
?>
<li><a href="<?php echo $page->url  . $cat->name; ?>"><?php echo $cat->title; ?></a>  /  </li>
<?php } ?>
<?php } ?>
</ul>

<?php } ?>

<div class="clear"></div><!-- /.clear -->

</div><!-- /#news_top_bar -->

<?php $news = $pages->find("template=news_entry, news_category_link=$category, limit=1");

foreach ($news as $entry) {

$class = "news_box";

if ($entry == $news->last()) { $class .= " news_box_last"; } ?>

<div class="<?php echo $class; ?>">

<div class="news_index_text">

<h3><a href="<?php echo $entry->url; ?>"><?php echo $entry->title; ?></a></h3>
<h6><?php echo $entry->entry_date; ?>  by  <?php echo $entry->createdUser->first_name . " " . $entry->createdUser->last_name; ?></h6>

<?php if ($entry->summary) {

echo "<p>" . $entry->summary . "</p>";

} else {

$body_text = strip_tags($entry->body);
$text = substr($body_text, 0, 300);
echo "<p>$text...</p>";
} ?>

<a href="<?php echo $entry->url; ?>" class="button">Read on</a>

</div><!-- /.news_index_text -->

<?php if ($entry->main_image) {

$news_image = $entry->main_image->size(240,180); ?>

<img src="<?php echo $news_image->url; ?>" width="<?php echo $news_image->width; ?>" height="<?php echo $news_image->height; ?>" alt="<?php echo $entry->title; ?>" class="small_frame" />

<?php } ?>

<div class="clear"></div><!-- /.clear -->

</div><!-- /.news_box -->

<?php }
?>

<?php // PAGINATION LINKS
$pagination = $news->renderPager();
echo $pagination;
?>

I've just re-read part of the pagination docs here and have added "start=0, limit=n" to my news_index template so now categories are showing even on 2nd page. Still can't seem to get pagination showing on categories template though

Edited by Soma

Share this post


Link to post
Share on other sites

Glad you figured out the start=0 thing for the news_index. When you are using pagination, PW will attempt to paginate all of your find() or children() function calls that have any limit=something. So adding start=0 is the way to keep them in check.

For the categories template, the only "limit=" statement that I see says "limit=1". In order for pagination to be activated, there has to be a limit greater than 1. You may be able to get the result you want by doing a $pages->find("limit=2, start=" . ($input->pageNum-1)); and then just using the first page it returns.

Share this post


Link to post
Share on other sites

Hi Ryan, thanks for the help I seem to have it working now, the limit=1 was just to try and start off pagination as a test because I only had 2 articles for any given category, but have now upped that and all seems well.

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 Sten
      Sorry I can't find any submit button to save my configuration on a template in the admin interface. The file is fine.
    • By nabo
      Hello
      this is my snippet
      public function init() { if($this->wire('user')->isSuperuser()) { $this->wire()->addHookAfter('ProcessPageEdit::buildFormSettings', $this, 'buildForm'); $this->wire()->addHookBefore('ProcessPageEdit::processInput', $this, 'saveForm'); } } public function buildForm(HookEvent $event) { $p = $event->object->getPage(); $inputfields = $event->return; $fieldset = $this->wire('modules')->get("InputfieldFieldset"); $fieldset->attr('id', 'my_fieldset'); $fieldset->label = __("My Renders"); $fieldset->collapsed = Inputfield::collapsedYes; $field = $this->wire('modules')->get("InputfieldTextarea"); $field->attr('name', 'renders'); $field->attr('value', $p->renders); $field->label = $this->_('Renders'); $fieldset->append($field); $inputfields->append($fieldset); } public function saveForm($event) { $page = $this->pages->get($this->input->post->id); $page->set('renders', $this->input->post->renders); } It builds correctly the inputs, I edit the field renders but when I save the page the value of this inputfield remain blank.
      What's wrong?
    • By carlitoselmago
      I'm building a fieldtype/inputfield to visually select a grid like Bootstrap since it's the first time coding such thing, I'm basing my code in FieldtypeColorPicker since the base structure is the same, a graphical UI helper to finally output a string as value.
      The problem is I try to save the value as something like "1,2,3" referencing the index of each column selected but it gets converted as an int value so "1,2,3" becomes "1" if input random text it gets converted as "0".
      Here is the code:
      https://github.com/carlitoselmago/FieldtypeGridSpace
      Here's an screenshot of how it looks

    • 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 bramwolf
      Hi Guys,

      I found that my site is producing this notice on the front end, even when $config->debug = false and also when admin is
      not even logged in. How do I keep the site from displaying this message to front end users? The notice is about a few vars
      being tested, which in this case aren't defined, how would I stop that from happing all together when testing vars? if I for instance
      wrap the $discountCode = blabla in a if($order) { ...  } statement than I'm again testing for $order which isn't a object in this case
      which throws another notice..

      This is the notice:


      Here is my current code:
      // PAD DISCOUNT CODE HOOK // First we see if we have active discount code if ($this->session->orderId) { $order = $this->pages->get($this->session->orderId); } if($product->geen_korting == 1) { $nodisc = $product->geen_korting; } else { $nodisc = $product->parent->geen_korting; } $discountCode = $this->sanitizer->selectorValue($order->pad_discount_code); $dc = $this->pages->get("template=paddiscount, title=$discountCode"); if ($dc->id) { if ($nodisc) { $discount = 0; } elseif ($dc->pad_percentage) { $discount = $newprice * ($dc->pad_percentage / 100); } } $event->return = $newprice - $discount;
      Thanks in advance 🙂
×
×
  • Create New...