thetuningspoon Posted April 20, 2012 Share Posted April 20, 2012 Hey guys, I think I'm really starting to get the hang of working with the ProcessWire api, following along with the cheatsheet. However, I'm drawing a blank on this. How do I test to see if the images field of a specific page contains any images? Same goes for the other field types. I know you can test for the existence of the field type on the page, but can you test for whether it has any data in it? Thanks! Link to comment Share on other sites More sharing options...
DaveP Posted April 20, 2012 Share Posted April 20, 2012 <?php if ($page->photo->eq(0)) { ?> <img src="<?php echo $page->photo->eq(0)->url; ?>" alt="<?php echo $page->photo->eq(0)->description; ?>"> <?php } else { ?> <img src="<?php echo $config->urls->templates; ?>pics/nophoto.png" alt="Sorry, no picture available"> <?php } ?> 1 Link to comment Share on other sites More sharing options...
onjegolders Posted April 20, 2012 Share Posted April 20, 2012 Everfree, there seem to be many different responses to this. It would be great if all the possibilities were listed in one reference page. Perhaps with the method and the types of field to which it applies. Link to comment Share on other sites More sharing options...
thetuningspoon Posted April 23, 2012 Author Share Posted April 23, 2012 Thanks, DaveP. I used if($page->images->eq(0) != null) { do something; } Was a little confused by your example until I read the explanation of eq() in the cheatsheet. Link to comment Share on other sites More sharing options...
Soma Posted April 23, 2012 Share Posted April 23, 2012 you can also use if($page->images->first()) ... Link to comment Share on other sites More sharing options...
ryan Posted April 23, 2012 Share Posted April 23, 2012 For a field that can contain multiple images, the value of that field is always going to be a type of array. So you'd have to either check to see if there are any items in the array, or try to retrieve the first time like Soma did. Here's how I usually check if there are any images: if(count($page->images)) { // images here } else { // no images } When it comes to a single image field, you are dealing with just one item that is either set or it isn't (rather than an array). You only need to check if it has a value: if($page->image) { // image here } else { // no image } When you use $pages->find(), $pages->get(), $page->children(), etc., you can also find pages that have a given amount of images. So if you wanted to find pages that don't have any images, you could do this: $pages->find("images.count=0"); ...or pages that have one or more images: $pages->find("images.count>0"); 6 1 Link to comment Share on other sites More sharing options...
onjegolders Posted April 23, 2012 Share Posted April 23, 2012 For a field that can contain multiple images, the value of that field is always going to be a type of array. So you'd have to either check to see if there are any items in the array, or try to retrieve the first time like Soma did. Here's how I usually check if there are any images: if(count($page->images)) { // images here } else { // no images } When it comes to a single image field, you are dealing with just one item that is either set or it isn't (rather than an array). You only need to check if it has a value: if($page->image) { // image here } else { // no image } When you use $pages->find(), $pages->get(), $page->children(), etc., you can also find pages that have a given amount of images. So if you wanted to find pages that don't have any images, you could do this: $pages->find("images.count=0"); ...or pages that have one or more images: $pages->find("images.count>0"); Thanks Ryan that clears it up for me! Link to comment Share on other sites More sharing options...
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