bwakad Posted September 19, 2014 Share Posted September 19, 2014 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 More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 - edit - I could only get it to work if testing for empty string: if($page->prev($browse) == '') { Link to comment Share on other sites More sharing options...
renobird Posted September 19, 2014 Share Posted September 19, 2014 find() returns a pageArray, so null will not work. Link to comment Share on other sites More sharing options...
renobird Posted September 19, 2014 Share Posted September 19, 2014 what about: if ($page->parent->next->id){ // next } if ($page->parent->prev->id){ //prev } 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 19, 2014 Share Posted September 19, 2014 Just for the record, if you've looked into the cheatsheet. NullPage != null Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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 More sharing options...
teppo Posted September 19, 2014 Share Posted September 19, 2014 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. 2 Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 19, 2014 Share Posted September 19, 2014 so if there is no ->id, NullPage is true. a NullPage returns an id of 0, so evaluating to false. Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 19, 2014 Share Posted September 19, 2014 There is an ID, but the id is 0, is different then "if NOT id" Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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 More sharing options...
LostKobrakai Posted September 19, 2014 Share Posted September 19, 2014 var_dump($pages->find("template=notAvailable")->id); // output: NULL So yeah, it's not 0 but NULL. Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 19, 2014 Share Posted September 19, 2014 (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 September 19, 2014 by Martijn Geerts Always fun those strict comparisons :-) Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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 More sharing options...
LostKobrakai Posted September 19, 2014 Share Posted September 19, 2014 Damned confused myself. "get()" was the one which either returns a NullPage or null, depending on where you call it, not "find()". Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 19, 2014 Share Posted September 19, 2014 @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 } 1 Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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 More sharing options...
LostKobrakai Posted September 19, 2014 Share Posted September 19, 2014 @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. 2 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 19, 2014 Share Posted September 19, 2014 Here's some reading about types bwaked: http://php.net/manual/en/language.types.php I hope that clarifies a bit. It will take some time to fully understand what is going on, but take your time, we all did. Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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... Link to comment Share on other sites More sharing options...
diogo Posted September 19, 2014 Share Posted September 19, 2014 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 More sharing options...
LostKobrakai Posted September 19, 2014 Share Posted September 19, 2014 A string is always TRUE , if it has chars in it! An empty string equals false. 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted September 19, 2014 Share Posted September 19, 2014 Good advise Bwaked: I will also set my set my mind to 0, with a new beer. Link to comment Share on other sites More sharing options...
bwakad Posted September 19, 2014 Author Share Posted September 19, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now