Jump to content

Image error


Peter Knight
 Share

Recommended Posts

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

Link to comment
Share on other sites

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()
Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
 Share

×
×
  • Create New...