Jump to content

checking for a field if else


wishbone
 Share

Recommended Posts

I want to check if there are elements uploaded in a field.

why does this simple code:

<?php
	if ($page->field_name){
		echo "<img src='{$config->urls->templates}ditib-img/dummy.gif' width='1200' height='600'>";
	}
	else{
		echo "<img src='{$config->urls->templates}ditib-img/dummy.gif' width='800' height='240'>";
	}
?>

only throw me the first choice? No matter if there are images uploaded to the (images type) field or not.

Link to comment
Share on other sites

An image field always holds a wireArray, wish returns true even if empty. To have a false in an if statement you want it to return false or 0.

if (count($page->field_name)){

This will return 0 in case the wireArray is empty.

  • Like 1
Link to comment
Share on other sites

On that example the field being checked has a maximum of 1 image:

Let's start by adding a new field in the admin interface (Setup -> Fields -> Add New Field). Name the field "sidebar_image", choose Image as the Type and label it "Sidebar Image". Submit. You'll now have a lot of options you can set for images. Change the Maximum files allowed to 1. We'll do multiple images later on. Submit.

In this case, the field returns the image itself or false NULL (in case it's empty) instead of an array.

edit:

//if empty

count($page->field_name) === 0

count($page->field_name)>0 === false

both forms are good :)

  • Like 2
Link to comment
Share on other sites

I looked again at the images examples on the docs and, as you told, Ryan also uses if($page->images) on a multiple images field. I tested it and, as expected, it does return true even if there is no image uploaded, making the test not very useful...

Ryan?

  • Like 1
Link to comment
Share on other sites

Yep, I have had that problem too. My workround was to look for the first() image (even though I am not using it as such) and check that. If no image is uploaded then it will return false.

I have a suspicion that if($page->images) on a multi images field (one that is set as having more than one possible image) that all you are checking is if the array is there - even if it is an empty array. Or something like that.

Me long-haired hippy muso - there is no such thing as emptiness!

  • Like 1
Link to comment
Share on other sites

I looked again at the images examples on the docs and, as you told, Ryan also uses if($page->images) on a multiple images field. I tested it and, as expected, it does return true even if there is no image uploaded, making the test not very useful...

Sounds like a typo, is that would only work on an image field set to contain a max of 1 image. Where did you find it?

Link to comment
Share on other sites

Thanks Diogo, I've corrected that. Those quick start tutorials were written by somebody that never finished them... and they were written back for the original PW 2.0, so  I'm not surprised there might be errors in there. I need to take some time to go through these for QA, because I don't think the author is going to finish them. But people seem to like them... every time I remove them, somebody gets upset. :)

  • Like 2
Link to comment
Share on other sites

  • 3 months later...

I've got a template which creates a list of pages using the following it only returns "Logo not supplied" even if the image (logo) has been loaded.

 foreach($result as $child) {
        echo "<li>";
        echo "<a href='{$child->url}'>{$child->title}</a>";
               $image = $pages->logo;
            if (count($pages->logo)){
                echo "Logo supplied";
            }
            else{
                echo "Logo not supplied";
            }
        
        echo "</li>";
    }
 

I'm presuming it has something to do with the list being children.

Link to comment
Share on other sites

In the above scenario, I also need to output the total of combined fields called "amount_approved" - where would I put something like the following - on the list but referencing child pages?

$total = 0;
        echo "<ol>";
        foreach ($page->grants as $grant) {
          echo "<li>{$grant->cheque_date} ";
          echo "<span>\${$grant->amount_approved}</span></li>";
          $total += $grant->amount_approved;
        }
        echo "</ol>";
        echo "<h3>Total grants: <span>\${$total}</span></h3> ";
Link to comment
Share on other sites

I'm not sure we can answer this without seeing the bigger picture here. But since your code is iterating through a property of $page, and not referencing other variables you had like $child or $result, chances are you can put this wherever you want.  It looks to me like it should be outside of any other loops. I would just put it where you need it. 

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