alejandro Posted November 20, 2013 Posted November 20, 2013 Hello, using the code $fwd = $pages->get("title=$transaction_id"); if($fwd) { $session->redirect($fwd->url); } else { $p = new Page(); $p->parent = $pages->get("1350"); $p->template = $templates->get("receipt"); $p->title = $transaction_id; $p->email = $email; $p->fcproduct = $product; $p->fcprice = $price; $p->save(); echo $p->render(); } I'm trying to generate a new page checkin if exists previosly. If exists is redirected correctly, if it doesn´t the page it's not created. But using just: $p = new Page(); $p->parent = $pages->get("1350"); $p->template = $templates->get("receipt"); $p->title = $transaction_id; $p->email = $email; $p->fcproduct = $product; $p->fcprice = $price; $p->save(); echo $p->render(); The page is created and rendered. I can´t get why the two pieces doesn´t like to be friends... Thanks in advance for any help, Alejandro
nik Posted November 20, 2013 Posted November 20, 2013 Hi Alejandro! That else-block is never executed, that's why the page does not get created and rendered. This is because $pages->get() "Returns a Page, or a NullPage if not found.". So $fwd is always an object (either a Page or a NullPage) and "if($fwd)" is always true. Test for the page id to see if you got a real page, like this: if($fwd->id) { ... } else { ... } (And remember to sanitize $transaction_id if it's part of user input.) 2
Wanze Posted November 20, 2013 Posted November 20, 2013 Change your if to: if ($fwd->id) { //... } Because if there's no page found, Pw returns a NullPage object (with id=0). Your current if is always true, because $fwd is an object. Edit: nik 1, wanze 0 2
alejandro Posted November 20, 2013 Author Posted November 20, 2013 Thank you Nik and Wanze (almost synchronized replies! ) It works as you both point. I think i have to read the API documentation paying more attention: http://processwire.com/api/types/nullpage/ So if I understand correctly, if " $fwd->id " is empty, NullPage is not returned.
Wanze Posted November 20, 2013 Posted November 20, 2013 So if I understand correctly, if " $fwd->id " is empty, NullPage is not returned. Almost If there is a page found, a Page object is returned, which always has an id > 0. If there is no page found, a NullPage object is returned, which always has the id = 0. the id is either equal to zero or greater, but never empty. 3
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