Jump to content
999design

Pagination - Limit an existing array

Recommended Posts

Hi,
 
Run into a bit of an issue with pagination on a site I am working on. I am sure the answer is really simple, but struggling to figure out what's going wrong.
 
I have a bunch of articles, but I only want the ones where the date is less than the current date.
 
I then want to limit the array so that I can paginate it.
 

<?php
 
$allNews = $page->children("template=news");
$oldNews = new PageArray();

foreach($allNews as $article) :
    if(strtotime($article->date) <= strtotime('now')):
        $oldNews->import($article);
    endif;
endforeach;

$oldNews = $oldNews->find('limit=2, sort=date');

echo $oldNews->renderPager();
                
foreach ($oldNews as $article) :
...
endforeach;

?>

The limit is being applied, however I don't see any pagination and navigation to the different pages returns the same first two articles.

Any help would be much appreciated.

Thanks,

Craig

Share this post


Link to post
Share on other sites

Hi, and welcome to the forums!

When paginating, you need to put the date check in the initial pages selector. You should do that anyway because it is much more efficient to only get the pages you need in the first place, rather than getting them all and limiting later on.

$allNews = $page->children("template=news, date<=".strtotime('now'));

Keep in mind that I think you also have another issue - using strtotime('now') or time() will always get all articles. You want to be comparing date to date, not date to a timestamp.

Share this post


Link to post
Share on other sites

You want to be comparing date to date, not date to a timestamp.

@adrian, when used in a selector, doesn't a datetime field return the unformatted value, i.e. a timestamp? So it should be okay to compare it with a supplied timestamp.

Also, as I've been learning recently, you can use relative time words directly in a $pages->find() selector (but not in PageArray filter selectors)

$allNews = $page->children("template=news, date<=now"); // edit: this doesn't work

Edit: ha, and now that I pay attention I see that of course that isn't a $pages->find() selector, so would need be written differently to have the minor convenience of using relative time in the selector :) :

$allNews = $pages->find("parent=$page, template=news, date<=now");

Share this post


Link to post
Share on other sites

@adrian, when used in a selector, doesn't a datetime field return the unformatted value, i.e. a timestamp? So it should be okay to compare it with a supplied timestamp.

Not if your field is set to only store the date, and not the date and time. ie, if it looks like this:

post-985-0-56852900-1462491029_thumb.png

In this situation, the date gets stored as: 2015-05-5. There is no way to store a date (without the time) as a timestamp. So when you go to "find" using a selector, you need something like:

$pages->find("date<".date("Y-m-d"));

or:

$pages->find("date<today");

Share this post


Link to post
Share on other sites

I have a date-only field configured as below and its unformatted value returns as a timestamp.

post-2897-0-45339900-1462491924_thumb.pn

post-2897-0-44928100-1462491928_thumb.pn

  • Like 1

Share this post


Link to post
Share on other sites

I have a date-only field configured as below and its unformatted value returns as a timestamp.

Apologies - you are correct - I don't think I ever realized PW did that. If you look at the database tables for a date field (without time) it is stored as Y-m-d, so I guess I assumed it would return just that. Note that it returns the timestamp for 12:00:00AM GMT that day.

By contrast, a datetime field is stored as a timestamp so I would expect a timestamp to be returned as the unformatted value.

  • Like 1

Share this post


Link to post
Share on other sites

Hi guys,

Thank for the replies, works a treat. Thanks for the extra info on the date time stuff.

Thanks,

Craig

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 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 ngrmm
      i have a event-page with a table.
      first column in this table is page-reference-field
      how can i find out if a urlSegment matches one of the rows having the same page-reference-field (page-id)?
      // all guests $allguests = new pageArray(); foreach($page->event_guests_table as $event_guests_table_row) { $allguests->prepend($event_table_row->guest); } // echo allguests would output: 1101|1102|1103|… // domain.tld/event/1101/ // show content if guest is in the table or redirect if($input->urlSegment1 ~= $allguests ) { // show content } else { // redirect }  
      which selector operator do i have to use? the one above does not work
    • 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?
    • By brdje
      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' )); }
      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.
       
       
×
×
  • Create New...