Jump to content

Recommended Posts

Posted

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

Posted

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

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?

 

 

Posted

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... :rolleyes:

  • Like 1
Posted
26 minutes ago, ottogal said:

 

You could replace this by


if($image) echo"

Edit: Like in the code you cited first... :rolleyes:

That what I thought but it was throwing an error. :-/

Posted

What needs to be checked depends on the field's Formatted value setting:

Formatted_value.thumb.png.27e8962b0d8d04e6aec1d1a6d167ae28.png

So when there is the possibility of getting null the code must account for it, you cannot just call first() "first" :) 

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
×
×
  • Create New...