Jump to content

how to check a field has value ?


adrianmak
 Share

Recommended Posts

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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)){}
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

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){}
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...