Forum Moderators: coopster

Message Too Old, No Replies

using IF with main image and variants

         

ski442

12:27 am on Dec 23, 2017 (gmt 0)

10+ Year Member



Hi Guys,
On my site, i have a main product page, showing the product details along with a main image, all details are pulled from a database, now i would like to display variants of this product, in a thumbnail underneath, so im using and IF statement to pull the variants from the image folder in the root.

<div id="thumbs">
<?php if ($Sstockcode) {
echo "<img src='/pictures/silverline/".$Sstockcode."_ENWNMPRO1.JPG' width='120' height='120' />";
echo "<img src='/pictures/silverline/".$Sstockcode."_ENWNMPRO2.JPG' width='120' height='120' />";
echo "<img src='/pictures/silverline/".$Sstockcode."_ENWNMPRO3.JPG' width='120' height='120' />";
echo "<img src='/pictures/silverline/".$Sstockcode."_ENWNMPRO4.JPG' width='120' height='120' />";
echo "<img src='/pictures/silverline/".$Sstockcode."_ENWNMPRO5.JPG' width='120' height='120' />";

}?>

</div>

as you can see there is upto 5 variations, but not all the products have variations, so if a product comes up with 3 variations, the code above works but it also shows the outline of a broken image thumbnail after the 3 variations.

How do i show only the variations needed without the broken img boxes.

I hope i have given you enough info, also im way out of practice now, its been years since i done this. any help would be great.
Thanks in advance.
Joe

lucy24

1:52 am on Dec 23, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



there is up to 5 variations
Is the number of “variations” stored in a database somewhere, or are you looking at a solution where the server has to physically look in the directory to see how many files with name in the form {suchandsuch} there are?

Now, if it were me I’d make
"<img src='/pictures/silverline/".$Sstockcode."_ENWNMPRO"
into a single string that’s formed once, at the beginning of the loop, and then you'd have a for/next structure that's conceptually
for x = number of "variations"
echo the-whole-longer-string + x + remainder-of-string

not2easy

1:52 am on Dec 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I'm not a php wiz or anything, but I believe that if you are using "if" you can add in "else" and then "endif" as in
<?php else: ?>
<?php endif; ?>

Don't worry, there are many more proficient with PHP than I, even on weekends, someone is likely to have a more definite answer.

phranque

2:58 am on Dec 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Is the number of “variations” stored in a database somewhere, or are you looking at a solution where the server has to physically look in the directory to see how many files with name in the form {suchandsuch} there are?

this is a key question.
if you have to check for the existence of 5 files for each request it can be inefficient.

topr8

7:54 am on Dec 23, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i don't even know why you are using the if statement ... surely every page will have a Sstockcode if it is a product page?
if so it will always be true and therefore pointless.

as said above regarding the number of images, either somewhere in the database the number of variant images has to be stored in which case a simple loop like Lucy suggested would be good or you'd have to check the folder to see if they exist which asphranque said is somewhat inefficient.

also do you know all the images are already 120 x 120? that's pretty small IMO almost pointless ... on most mobile devices you'd barely be able to see them.
(or are the images bigger but you want to show them that size - if so, of course the originals are square? - this is also very inefficient as the user is downloading big inages only to see them small)

lucy24

5:54 pm on Dec 23, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



on most mobile devices you'd barely be able to see them
If you've got the appropriate headers in place--width=device-width and so on--it should make no difference.

surely every page will have a Sstockcode if it is a product page?
To me the line sounds more like insurance. Any time you've got a function involving the value of x, you first make sure that x has a value. (Unless 0 is one possible value, which is obviously not the case here.)

If you know that there are always at least three variations, then at least you only need to check for nos. 4 and 5. (Is there a maximum number, or do you have to keep checking until you run out?) I'm thinking of those hover-for-closeup images where there's a main picture and a few thumbnails showing different aspects of the product.

topr8

10:08 pm on Dec 23, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>>If you've got the appropriate headers in place--width=device-width and so on--it should make no difference.

fair enough, and if the pics are actually 1000px square, then yes you can resize them on mobile!
but likewise if your pages come in at 10mb each! you are also burning through someone's data allowance pretty quickly!