Jump to content

PageArray vs explode


fenton
 Share

Recommended Posts

Hi there,

I feel stupid for asking this

I'm trying to loop through folders with pages that have the template "articles" and want to display "prev I next" buttons, but don't want to display the pipe symbol on the first ot last post. I had it working with the explode version (uncommented code), but it doesn't work if I do it the right way with the PageArray. I'm referring to this post: http://processwire.com/talk/topic/3865-image-tags/?p=37806

<?php
            
            $siblings = $pages->find("template=article");
            $next = $siblings->getNext($page);
            $prev = $siblings->getPrev($page);
            
            /*
            $pageID = $page->id;
            $siblings_array = explode("|", $siblings);
            $first_page = reset($siblings_array);
            $last_page = end($siblings_array);

            if ($pageID == $last_page) {
                echo "<a href='$prev->url'>prev</a>";
            }
            elseif ($pageID == $first_page) {
            echo "<a href='$next->url'>next</a>";
            }
            else {
                echo "<a href='$next->url'>next</a> | <a href='$prev->url'>prev</a>";
            } */
            
            $pageID = $page->id;
            $first_page = $siblings->first();        
            $last_page = $siblings->last();
            
            if ($pageID == $last_page) {
                echo "<a href='$prev->url'>prev</a>";
            }
            elseif ($pageID == $first_page) {
            echo "<a href='$next->url'>next</a>";
            }
            else {
                echo "<a href='$next->url'>next</a> | <a href='$prev->url'>prev</a>";
            }
            
            ?>
 

any ideas what I'm doing wrong?

thanks a lot, j

Link to comment
Share on other sites

Even these: $next = $siblings->getNext($page); $prev = $siblings->getPrev($page); are verbose I think? PW already has $page->next(); and $page->prev(); Can't those be used in this case?

See these (answer for you in first link):

http://processwire.com/talk/topic/3133-detect-first-and-last-sibling-page/
http://processwire.com/talk/topic/3714-how-to-do-this/

http://processwire.com/api/types/nullpage/

Edited by kongondo
Link to comment
Share on other sites

There's an easy way to strip off chars with php method trim(). But in this case it can get tricky.

Also regarding PageArray and explode doesn't really play a role here... :) But it sinks in better if you make faults.

I think an idea would be to do:

$nav = '';
if($page->prev->id) $nav .= "<a href='{$page->prev->url}'>prev</a> | ";
if($page->next->id) $nav .= "<a href='{$page->next->url}'>next</a> | ";

echo trim($nav," | "); 

Fairly easy concept.

Depending on how and where you use it, this way it can get annoying as you get jumping link elements. You get sometimes "prev" sometimes "prev|next" links

So maybe add some logic to show only a span when there's no next found. So you can fade it out with CSS. Now this way there's no need to trim.

$nav = '';
if($page->prev->id) $nav .= "<a href='{$page->prev->url}'>prev</a> | ";
    else $nav .= "<span class='inactive'>prev</span> | ";
if($page->next->id) $nav .= "<a href='{$page->next->url}'>next</a>";
    else $nav .= "<span class='inactive'>next</span>";

echo $nav;
  • Like 1
Link to comment
Share on other sites

This can get really creative, as Ryan would now maybe come with something like:

$nav = '';
foreach(array("prev","next") as $dir) {
    if($page->$dir->id) $nav .= "<a href='{$page->$dir->url}'>$dir</a> | ";
        else $nav .= "<span class='inactive'>$dir</span> | ";
}
echo trim($nav, " | ");
  • Like 3
Link to comment
Share on other sites

thanks a lot guys!

But it sinks in better if you make faults.

true :)

PW already has $page->next(); and $page->prev(); Can't those be used in this case?

the problem is, that I have to traverse folders, since my structure is like this:

News

- 2013

-- 05

--- article 06

--- article 05

--- article 04

--- article 03

--- article 02

-- 06

--- article 01

still wondering though, why from my example above:

if ($pageID == $last_page) {
                echo "<a href='$prev->url'>prev</a>";
            }
 

doesn't work, even though if I echo both variables the value is the same: 1060 1060

cheers, j

Link to comment
Share on other sites

I know, I know ...it's people like me that give PHP a bad name ;)

e.g. if I'm on the first article "news/2013/06/newest-post" I can use "prev" till it reaches the last article in "news/2013/06/", it doesn't traverse the folders ( to "news/2013/05/older-post" )

Link to comment
Share on other sites

Yeah prev and next does only traverse inside a parent. But there's plenty traversal methods to get around it.

$nav = '';
if($page->prev->id) {
    $nav .= "<a href='{$page->prev->url}'>prev</a> | ";
} else if($page->parent->prev->id && $page->parent->prev->is("template=month") && $page->parent->prev->numChildren){
    $nav .= "<a href='{$page->parent->prev->children->last->url}'>prev</a> | ";
}
if($page->next->id) {
    $nav .= "<a href='{$page->next->url}'>next</a> | ";
} else if($page->parent->next->id && $page->parent->next->is("template=month") && $page->parent->next->numChildren){
    $nav .= "<a href='{$page->parent->next->children->first->url}'>next</a> | ";
}

echo trim($nav," | ");

Maybe need to adapt to your situation, but it's straight forward and everything is possible.

  • Like 2
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...