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 21, 2013

Ok so how the images are stored are as follows:

Tables

Images:              |    Post2Images               |   Post:

ID      Filename |    Image_ID     Post_ID  |    ID     Name     Description

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

1       Image1     |     1                    1             |    1       Post1     This is description of post 1

2       Image2     |     2                    1             |

3      Image3      |     3                    1             |

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

4     Photo1        |    4                    2               |    2     Post2       This is description of post 2

5     Photo2        |    5                    2               |

So the above example is 2 different posts with Post 1 has 3 associated images, Post 2 has 2 associated images.

In my description I put placeholders [image1], [image2] ect. And with the origional post above I created an array with [image2] [image3] that will find those in the description field and replace those with variable $image2 and $image3 where those variables are as follows:

$photo_post = getPhotoPostByLinkname($Read, $_GET[post]);

$pix1 = getRelatedPhotos1($Read, $photo_post['post_id']);

$pix2 = getRelatedPhotos2($Read, $photo_post['post_id']);  

$pix3 = getRelatedPhotos3($Read, $photo_post['post_id']);

Where each function is as follows:

function getRelatedPhotos1($read, $post_id) {

$sql = "SELECTimages.photo_id, filename

   FROM images

   INNER JOIN Post2Images USING (image_id)

   WHERE post_id = $post_id

   LIMIT 0, 1";

return $read->fetchAll($sql);

}

And then:

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

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

<?php

$image2 = $picture2['filename'];

$image3 = $picture3['filename'];

I hope this will help explain my method and what I am trying to accomplish.


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