Jump to content

navigate prev and next


bwakad
 Share

Recommended Posts

For mobile layout I want to make use of the functions provided by PW: prev and next. So I have this, but it will not display both. Can someone point me in the right direction?

<ul class="inline-list">
<?php
     $browse = $pages->find("parent=/browse/");
     if($page->prev($browse) == null) {
        echo "<li><a href='{$page->url}'>{$page->name}</a></li>";                            
     } else {
     echo "<li><a href='{$page->prev($browse)->url}'>{$page->prev($browse)->name}</a></li>";
     }                            
                            
     if($page->next($browse) == null) {
        echo "<li><a href='{$page->url}'>{$page->name}</a></li>";
     } else {
        echo "<li><a href='{$page->next($browse)->url}'>{$page->next($browse)->name}</a></li>";
     }                            
?>
</ul>
Link to comment
Share on other sites

find() returns a pageArray, so null will not work.

but in the docs:

http://processwire.com/api/variables/page/

$page->next($pageArray)

Given a PageArray which includes the current page (among others), return the next page after the current.† Returns a NullPage if it is the last page in the provided PageArray. If called without a PageArray, it assumes the current page's siblings (same as $page->next).

Then why should null not work ???

ps: NullPage != null .... were in cheatsheat ?

Link to comment
Share on other sites

Maybe I do not get what you say, but from what I do understand from the pageArray (limited amount of pages) >>> $browse = $pages->find("parent=/browse/") -

the next code would produce a NullPage if there is no previous, there would be none (no id), because I used === "0" == "0" and = "0" ... and it did not work.

if(!$page->prev($browse)->id ) {
Link to comment
Share on other sites

@LostKobrakai, a WireArray has no id, thus resulting in NULL
 

$nullpage = new NullPage;

var_dump($nullpage->id); // (int) 0
if ($nullpage->id == false) {
    // is true
}

if ($nullpage->id === false) {
    // is not true
}
Edited by Martijn Geerts
Always fun those strict comparisons :-)
Link to comment
Share on other sites

you all want to confuse me :'(

Lostkobrakai - is right when he say is not 0 but NULL. But checking for that did not solve it.

Martijn - is right when $nullpage = new NullPage, hence id = 0

Teppo is right when he say NullPage is not Null.

but NOW I don't know anymore!!!

Link to comment
Share on other sites

@bwaked:

You have Loose comparisons, and you have strict comparisons.

loose are written with two operators like ==

strict is written with three operators like ===

Strict will look next to value also to the type.

I will write it here in text rather then in values:

if (string === integer) {

    // a string is never an integer, thus evaluate to false

}

  • Like 1
Link to comment
Share on other sites

So, if I want to evaluate if there is none - which one is the best to use... I prefer your example of == false, but is this always accurate with regards to a NullPage ?

if($page->prev($browse)->id == false ) {
// display somthing else
}

or

if(!$page->prev($browse)->id === true ) { // which is rather confusing
// display somthing else
}

or

if(!$page->prev($browse)->id ) {
// display somthing else
}
Link to comment
Share on other sites

@bwakad

Essentially, if you work with pages, there are two options. Either you expect one page or a failure or secondly one or more pages or failure.

$single_page = $pages->get("something");

if($single_page->id) echo "Page found";

$multiple = $pages->find("something");

if(count($multiple)) echo "at least one page found";

The first check works, because get() returns a NullPage on failure, which has an id of 0. 

The second one counts the found PageArray elements. If there are none it's also 0. 

The difficulty is not to confuse those (as I did), as NullPage is something completely different as a PageArray / WireArray. 

  • Like 2
Link to comment
Share on other sites

the next code would produce a NullPage if there is no previous, there would be none (no id), because I used === "0" == "0" and = "0" ... and it did not work.

Just don't forget that "0" != 0 and "0" != FALSE because it's a string. A string is always TRUE

Link to comment
Share on other sites

Just don't forget that "0" != 0 and "0" != FALSE because it's a string. A string is always TRUE

I just see on PHP that checking on ->id is integer (martijn said that to) while I used =>id =, == and === "0". I now understand why it did not work...

This one tumbled me:

// the easiest way…if(!$page->id) {    // $page is a NullPage} 

I used it so I could display something else, but I forgot the ->id... which makes it an object, and I did not know how to check an object.

So my best bet is either

if(!$page->prev($browse)->id) { OR if($page->prev($browse)->id == false ) {

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...