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.

Participating Frequently
March 21, 2013

So now attemptying to loop through it I have made a few changes, which seems to be improving but am still not having the correct output.

This is Post 2 which has 2 related images as shown above. The description is as follows:

------------------------

The description....

[image]

[image]

------------------------

Instead of putting a number in each image, I am now just calling them all [image]. I don't know if this is more effective but what is now happending is as shown bellow: If echoes the description and displayes the first image at the two [image] place holders, then loops through and echoes the description again this time with the second image at the two [image] place holders.

------------------------

The description...

[image1]

[image1]

The description...

[image2]

[image2]

------------------------

And here is the code I am working with...

    <?php foreach ($photos as $picture) { ?>

    <?php

      $count = count($picture);

      for($i = 1; $i<= $count; $i++) {

    $image[$i] = $picture['filename'];

    $image_path = '../../content_management/image_upload/';

    $placeholders = array("[image]");

    $image_location = array('<a class="fancybox" href="' . $image_path . '' . $image[$i] . '" data-fancybox-group="gallery"><img src="' . $image_path . '' . $image[$i] . '" /></a>');

}

    $rawstring = $photo_article['description'];

    $new_string = $rawstring;

    $new_string = str_replace($placeholders, $image_location, $new_string);

    echo $new_string;

    ?>

    <?php } ?>


Sorry, been busy and won't get a chance to read your replies till later. Another thing that would help is a link to your site to see the layout, or at least an annotated screen shot.