Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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 } ?>
Copy link to clipboard
Copied
>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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
>Can you please go into more detail?
I'm suggesting using a multidimensional array to store the details associated with each image. Of course, you may not even need this - why can't you just loop through your recordset?
>I have tried another way by having
>variable $image = all image in query.
Not sure what you mean. How are you storing multiple images in a single variable?
>Now should I count the images or rows in this
>variable? And if so do what with them?
Really need to know more about how your data is structured and what you are doing with it.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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 } ?>
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Here is my best at a sreen shot. It's zoomed out..
.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now