Check if images exist, else null
I have images relating with each post. Some only 1 and as many as 10. But the problem is I am creating a variable for each image ($pix1, $pix2 etc.), and if say the post or article has only one image linked to it, it doesn't echo the full description... Here is the code which I am defining the varibles and replacing the placeholders with the corresponding variable.
<?php foreach ($pix2 as $picture2) { ?>
<?php foreach ($pix3 as $picture3) { ?>
<?php
$image2 = $picture2['filename'];
$image3 = $picture3['filename'];
$image_path = '../../content_management/image_upload/';
$placeholders = array('[image2]', '[image3]');
$image_location = array('<a class="fancybox" href="' . $image_path . '' . $image2 . '" data-fancybox-group="gallery"><img src="' . $image_path . '' . $image2 . '" /></a>', '<a class="fancybox" href="' . $image_path . '' . $image3 . '" data-fancybox-group="gallery"><img src="' . $image_path . '' . $image3 . '" /></a>');
$rawstring = $featured_article['description'];
$new_string = $rawstring;
$new_string = str_replace($placeholders, $image_location, $new_string);
echo $new_string;
?>
<?php } ?>
<?php } ?>
So what is happening is that a new post has only two images associated with it, it doesn't post the description. As in the description I am puting placeholders of where I want the image for example "[image2]". And that way the placeholder [image2] will be replaced with the second image. But since it doesn't have a third image it doesn't post the description field.
Is there any functions I can use to check if the $pix variables have an image defined? Or any input on the best way to check this? Any help would be great!
Thanks,
Riley
