Grow your CSS skills. Land your dream job.

Reverse PHP Increment

  • # January 7, 2013 at 9:35 pm

    I’m trying to display a number next to each article with the following code in Kirby.

    < ?php
    $count = 0;
    foreach($page->children()->visible()->flip() as $article):
    $count++
    ?>

    < ?php endforeach ?>

    However, it’s outputting like this

    `1 Latest Article`

    `2 Older Article`

    I need it to output like this

    `2 Latest Article `

    `1 Older Article`

    # January 7, 2013 at 9:53 pm

    Maybe set the counter to the highest number, and count down.

    I took a peek at the Kirby API cheat sheet I came up with this.

    < ?php
    $count = $page->count();
    foreach($page->children()->visible()->flip() as $article):
    $count–;
    ?>

    http://getkirby.com/content/02-docs/kirby-cheatsheet.pdf

    # January 7, 2013 at 9:57 pm

    < ?php
    $articles = $page->children()->visible()->flip();
    $count = $articles->count();
    foreach($articles as $article):
    ?>

    < ?php
    $count–;
    endforeach
    ?>

    # January 7, 2013 at 9:57 pm

    @hotpink I had to change your code as it was counting all the pages, I think (it started from 16).

    $count = $page->children()->count();

    This echos out 0 for the first article where I need it to echo out 1.

    # January 7, 2013 at 9:58 pm

    @hotpink is correct, but the page count will end in zero. Subtract at the end of the loop to end with one.

    # January 7, 2013 at 9:58 pm

    Nice @benwalker. Thank you!

    Edit: Also, thank you @hotpink

    # January 7, 2013 at 9:59 pm

    Actually, scrap that. My code is the one to use :-)

    … I think

    # January 7, 2013 at 10:02 pm

    @BenWalker Your code works perfectly.

    # January 8, 2013 at 1:27 am

    down

    # January 8, 2013 at 1:31 am

    wow, code parser is terrible on this site. nvm my help

    http://pastebin.com/h7aCGq0d

Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".