Peter Knight Posted June 13, 2017 Posted June 13, 2017 I have a page that's throwing an error because some of the pages in the for each loop don't have an image Quote Fatal error: Call to a member function first() on null in The docs say it's wise to check that an image has content and only then try to output something. I thought the following would work with the if($image) below. $products = $page->children(); foreach ($products as $prod){ $image = $prod->images->first(); echo" <div > <div class='prod-ov-wrapper'>"; if($image) echo " <a href='$prod->url'> <img src='{$image->height(160)->url}' class='prod-preview'> </a>"; echo" <div class='prod-ov-text'> <strong>Part No:</strong>{$prod->title} <br/> {$prod->prod_summary} <a href='$prod->url' class='uk-icon-button' uk-icon='icon: chevron-right'></a> </div> </div> </div> ";} What am I overlooking here? Thanks
ottogal Posted June 13, 2017 Posted June 13, 2017 Obviously the error is thrown already with the line $image = $prod->images->first(); (not only when you try to echo the image). So you should do a check for $prod->images before you apply ->first()
Peter Knight Posted June 14, 2017 Author Posted June 14, 2017 Thanks @ottogal Picking my way through this but that makes sense. I've never really set a conditional that far up the php script but looking around the forums, this seems to be correct. <?php $products = $page->children(); foreach ($products as $prod){ // Check that images field is poulated if ($prod->images) { //Create variable from first image in field $image = $prod->images->first();} echo" <div > <div class='prod-ov-wrapper'>"; // Check that images field is poulated if($prod->images) echo " <a href='$prod->url'> <img src='{$image->height(160)->url}' class='prod-preview'> </a>"; echo" <div class='prod-ov-text'> <strong>Part No:</strong>{$prod->title} <br/> {$prod->prod_summary} <a href='$prod->url' class='uk-icon-button' uk-icon='icon: chevron-right'></a> </div> </div> </div> ";} ?> I'm wondering though if I'm being a little verbose. I am checking twice if the image field is populated and I have a feeling I could / should only need to do this once?
ottogal Posted June 14, 2017 Posted June 14, 2017 Your second use of if($prod->images) echo " is unnecessary (since you get only to this point if the condition is true). You could replace this by if($image) echo" Edit: Like in the code you cited first... 1
Peter Knight Posted June 14, 2017 Author Posted June 14, 2017 26 minutes ago, ottogal said: You could replace this by if($image) echo" Edit: Like in the code you cited first... That what I thought but it was throwing an error. :-/
szabesz Posted June 14, 2017 Posted June 14, 2017 What needs to be checked depends on the field's Formatted value setting: So when there is the possibility of getting null the code must account for it, you cannot just call first() "first"
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