Jump to content

Pagination - Limit an existing array


999design
 Share

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

Link to comment
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.

Link to comment
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");
Link to comment
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");
Link to comment
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
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...