Jump to content


Photo

How do I test for images?


  • Please log in to reply
6 replies to this topic

#1 everfreecreative

everfreecreative

    Sr. Member

  • Members
  • PipPipPipPip
  • 109 posts
  • 43

  • LocationCT, USA

Posted 20 April 2012 - 10:27 AM

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!

#2 DaveP

DaveP

    Sr. Member

  • Members
  • PipPipPipPip
  • 287 posts
  • 136

  • LocationChorley, UK

Posted 20 April 2012 - 11:03 AM


<?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 } ?>


Twitter : Facebook : GitHub : G+ : Blog : Powered by C8H10N4O2 and C10H14N2

#3 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 20 April 2012 - 11:09 AM

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.

#4 everfreecreative

everfreecreative

    Sr. Member

  • Members
  • PipPipPipPip
  • 109 posts
  • 43

  • LocationCT, USA

Posted 23 April 2012 - 08:36 AM

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.

#5 Soma

Soma

    Hero Member

  • Moderators
  • 3,191 posts
  • 1745

  • LocationSH, Switzerland

Posted 23 April 2012 - 08:45 AM

you can also use

if($page->images->first()) ...

@somartist | modules created | support me, flattr my work flattr.com


#6 ryan

ryan

    Hero Member

  • Administrators
  • 5,773 posts
  • 3118

  • LocationAtlanta, GA

Posted 23 April 2012 - 12:12 PM

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");


#7 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 799 posts
  • 207

  • LocationMidlands, UK

Posted 23 April 2012 - 01:00 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users