adrianmak Posted April 13, 2015 Posted April 13, 2015 there is a optional image field of a template. If I put this code in a template, for page without uploaded an image , it will throw a php error of "Call to a member function size() on a non-object" $header_image = $page->img->size(800, 250)->url;
kongondo Posted April 13, 2015 Posted April 13, 2015 If field can contain only one value or a string... if($page->title) echo $page->title; If field can contain multiple values, e.g. a multiple image field or multiple page field (i.e. contain arrays) if(count($page->images)) echo "we have images. Let's foreach them"; https://processwire.com/talk/topic/2239-how-to-test-for-empty-fields/ https://processwire.com/talk/topic/546-how-to-detect-an-empty-field/ https://processwire.com/talk/topic/8067-cant-detect-empty-field/ https://processwire.com/talk/topic/957-how-to-find-elements-with-empty-field/ https://processwire.com/talk/topic/8850-check-if-a-datetime-field-is-empty-does-not-work/ https://processwire.com/talk/topic/4177-checking-for-empty-textarea-field/ https://processwire.com/talk/topic/3050-checking-for-a-field-if-else/ etc.... 2
Juergen Posted April 13, 2015 Posted April 13, 2015 Why dont you check the image with an if statement? if($page->img){ $header_image = $page->img->size(800,250)->URL; } Or am I misunderstand your question?
adrianmak Posted April 14, 2015 Author Posted April 14, 2015 Why dont you check the image with an if statement? if($page->img){ $header_image = $page->img->size(800,250)->URL; } Or am I misunderstand your question? I tried to use isset for checking, but it's not working. No more php error for image field without an image, however it returned no value even though there is an image in the field
kongondo Posted April 14, 2015 Posted April 14, 2015 I suggest you carefully study and bookmark/reference this table. It is an invaluable reference that will teach/remind you when to use/not use isset, empty, is_null, etc.. PHP type comparison tables http://php.net/manual/en/types.comparisons.php As for you image, I think I already answered you question but I'll repeat it here: If it is a single image field [an image field with a maximum files allowed of 1] if($page-img) {} if it is a multiple image field [an image filed with maximum files allowed of 0 or > 1, even if it contains only one image in your page] if(count($page->img)){} 2
adrian Posted April 14, 2015 Posted April 14, 2015 When you have a field that is returning an array, like an images field that allows more than 1 file, you want this: if(count($page->img)) { 1
Raymond Geerts Posted June 9, 2015 Posted June 9, 2015 On selector level you can select the pages where the image field is not empty asfollow. $pages_with_images = $pages->select("your_image_field!='', limit=25");
tpr Posted June 9, 2015 Posted June 9, 2015 Is there any performance difference between these? if(count($page->img)){} if($page->img->count()){} Lastly, is it safe to use "count" only and not "count()"? if($page->img->count){}
LostKobrakai Posted June 9, 2015 Posted June 9, 2015 They are essentially the same. The latter one is just a convenience function which does also just use php's count(). 1
tpr Posted June 9, 2015 Posted June 9, 2015 Thanks! So I should better use count($page->img) as it skips one - minor - step?
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