Jump to content

Recommended Posts

Posted

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

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 ?

Posted

NullPage is an object, kind of a placeholder for non-existing page, and that is very different from NULL (PHP's way of saying that a variable has no value at all). They're not the same thing, so NullPage == null is not true. It's that simple, really.

  • Like 2
Posted

Ohw. That's were I was off track, lol. Thanks, I understand now. The link to NullPage in docs helped! Apparantly I have to check for ->id

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

so if there is no ->id, NullPage is true.

Posted

a NullPage returns an id of 0, so evaluating to false.

thats what I said: if NOT id then NullPage = true

lol. if false = true then false = false, but if true = true then true = true.

Posted

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 ) {
Posted (edited)

@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 :-)
Posted

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

Posted

@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
Posted

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
}
Posted

@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
Posted

well, thanks all, for helping on this one - I will set my mind to 0 Null or blanc page (NullPage) and try to read from the link...

Posted

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

Posted

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 ) {

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
  • Recently Browsing   0 members

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