Jump to content

Having trouble combining sorting by date and paging


webweaver
 Share

Recommended Posts

I have a News listing page that is pulling from a list of articles and I'm trying to sort by date (descending) and limit the articles to four per page. I have attached a screenshot of the articles in the pw admin

post-1751-0-93947200-1392231619_thumb.jp

I have added their dates (pw field article_date) to the right. You can see that they are not physically in order. 

When I try to sort the children by date and limit to 4, it is actually taking the first 4 entries in pw and sorting them by date, then the next 4 and sorting by date. When what I want is sort ALL of the articles by date then display only 4 at a time. 

Here is my current code:

<?php $articles = $pages->get(1133)->children("limit=4")->sort("-article_date");
	foreach ($articles as $article) { 
	echo "<div style='font-size:.75em; margin:0; padding:0 5px;'>".$article->article_date."</div>";
    echo "<h4 style='margin:0; padding:0 5px; margin-bottom:20px;'><a href='".$article->url."'>".$article->title."</a></h4>";
    $summary = substr($article->article, 0, 600);
	if(($pos = strrpos($summary, ".")) !== false) {
		$summary = substr($summary, 0, $pos);
	}
	echo "<img style='float:left; margin-right:20px;' src='".$article->article_image->url."' />";
	echo "$summary ...";
    echo "<div style='clear:both;'></div>";
    echo "<div style='float:right;'><a class='project' href='".$article->url."'>Read More »</a></div>";
    echo "<div style='clear:both;'></div><hr>";
    } ?>

            <div class="clr"></div>
          </div>
          <div class="clr"></div>
        </div>

    </div>
    <div>   <?php  $pagination = $articles->renderPager();
    echo $pagination;
 ?></div>

If I take out the ("limit=4") and just do sorting, the sorting is correct. 

*also for some strange reason the article "Value Activation: a different business model" is not showing up at all - but that's a different issue.

Any help would be appreciated.

Link to comment
Share on other sites

$articles = $pages->get(1133)->children("limit=4")->sort("-article_date");

The problem is that here you are doing it in this order:

  1. children("limit=4") -> get the 4 first children
  2. sort("-article_date") -> sort them

When what you want to to is

  1. get all the children
  2. sort them
  3. limit them

Even better would be to do it with one selector so you can do all that at the same time and at the DB level.

Like this:

$articles = $pages->find(parent=1133, sort=-article_date, limit=4);
Link to comment
Share on other sites

@webweaver I see you use teflon. What PW version on? Just so you are aware, teflon doesn't support 2.3.~2 - 2.4 yet with all features like field dependencies add new button on home screen, not aware of any other atm. The new default admin theme is quite nice and has some taken from teflon (sort of).

Link to comment
Share on other sites

@Soma - yes, love teflon. I'm running pw 2.3 on servers that are not going to be upgraded to the lastest php or mysql - I have too many sites running scripts that will break if I upgrade mysql or php :( But I have already ordered a new dedicated server where I will install the latest php/mysql to run all of my future 2.4 pw sites :)

Once I install my first 2.4 site, I'll be curious to see if the new default admin still needs some more teflon :)

Link to comment
Share on other sites

sorry to interrupt the talk about teflon guys ;)

If you want a set of pages it's generally better to use find() and try to do everything with one selector.

It's actually a nice exercise to try to achieve complex sets of pages with only one selector :)

edit: what I'm calling "one selector" above, is not actually one selector, but a group of selectors in one find().

  • Like 1
Link to comment
Share on other sites

@Soma - I'm not sure. The site I posted the attachment for was the Teflon version I download in Sep '13. The lastest site I developed I downloaded your Teflon on 1/23/14 (somatonic-teflon-admin-theme-pw2-f87f6b6).

@Diogo - Thanks again - that clears things up for me and I now understand.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...