Skip to main content
Inspiring
March 20, 2013
Question

Check if images exist, else null

  • March 20, 2013
  • 1 reply
  • 1591 views

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

This topic has been closed for replies.

1 reply

Inspiring
March 20, 2013

I am thinking what I need to do is something similar to below. Marked in bold is the new code added, but doesn't work. If I am in the right direction please let me know!!! I also changed the variables in the $image_location array as well.

<?php foreach ($pix2 as $picture2) { ?> 

<?php foreach ($pix3 as $picture3) { ?>

<?php

$image2 = $picture2['filename'];

$image3 = $picture3['filename'];

if (isset($image2)) {

$image2 = $img2;

} else {

$image2 = NULL;

}

if (isset($image3)) {

$image3 = $img3;

} else {

$image3 = NULL;

}

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

Participating Frequently
March 20, 2013

>But the problem is I am creating a variable for each image ($pix1, $pix2 etc.)

Ugh. I would not do it that way. If you have a variable number of images, then just create an array to store the image path, descriptions and locations.

Inspiring
March 20, 2013

Can you please go into more detail?

I have tried another way by having variable $image = all image in query. Now should I count the images or rows in this variable? And if so do what with them?