Page 1 of 1
Foreach loop echo once - SOLVED
Posted: Thu Mar 15, 2012 7:28 pm
by squtternutbosh
PHP 101 time:
Have the following statement:
Code: Select all
<?php foreach ($images as $image) { ?>
<a href="<?php echo $image['popup']; ?>" title="<?php echo $heading_title; ?>" class="fancybox" rel="fancybox">Click here for large image</a>
<?php } ?>
Displays this:
image1.jpg (15.86 KiB) Viewed 2965 times
Need to display only the first "Click here for large image" as this links to a fancybox pop-up that can then be scrolled left or right to reveal more additional images (would be satisified with that solution) but to achieve this the code still needs to run just not echo the text link multiple times.
FYI: the additional images change the main 'zoomable' image when clicked hence the need for this extra link.
Guessing this is pretty simple PHP but am struggling to find an answer i can understand elsewhere so hope someone can help.
Version 1.5.1.2 - link:
http://oceanlifestyle.com/shop/Gifts/Na ... Gone-signs
Re: Foreach loop echo once
Posted: Fri Mar 16, 2012 5:32 am
by rph
Code: Select all
<?php if (isset($images[0]['popup'])) { ?>
<a href="<?php echo $images[0]['popup']; ?>" title="<?php echo $heading_title; ?>" class="fancybox" rel="fancybox">Click here for large image</a>
<?php } ?>
Re: Foreach loop echo once
Posted: Fri Mar 16, 2012 4:36 pm
by squtternutbosh
Thanks so much for your help, unfortunately i get this error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/a5877638/public_html/shop/catalog/view/theme/default/template/product/product.tpl on line 32
Here is the full code for the additional images section as it was:
Code: Select all
<?php } ?>
<?php if ($images) { ?>
<div class="image-additional">
<?php foreach ($images as $image) { ?>
<a href="<?php echo $image['popup']; ?>" title="<?php echo $heading_title; ?>" class="cloud-zoom-gallery" rel="useZoom: 'zoom1', smallImage: '<?php echo $image['thumb']; ?>' ">
<img src="<?php echo $image['thumb']; ?>" width="74" height="74">
</a>
<?php } ?>
<br>
<?php foreach ($images as $image) { ?>
<a href="<?php echo $image['popup']; ?>" title="<?php echo $heading_title; ?>" class="fancybox" rel="fancybox">Click here for large image</a>
<?php } ?>
</div>
<?php } ?>
</div>
<?php } ?>
And with the new suggested code below: maybe i just put it in the wrong place?
Code: Select all
<?php } ?>
<?php if ($images) { ?>
<div class="image-additional">
<?php foreach ($images as $image) { ?>
<a href="<?php echo $image['popup']; ?>" title="<?php echo $heading_title; ?>" class="cloud-zoom-gallery" rel="useZoom: 'zoom1', smallImage: '<?php echo $image['thumb']; ?>' ">
<img src="<?php echo $image['thumb']; ?>" width="74" height="74">
</a>
<?php } ?>
<br>
<?php if (isset($images[0]['popup'])) { ?>
<a href="<?php echo $images[0]'popup']; ?>" title="<?php echo $heading_title; ?>" class="fancybox" rel="fancybox">Click here for large image</a>
<?php } ?>
</div>
<?php } ?>
</div>
<?php } ?>
Re: Foreach loop echo once
Posted: Fri Mar 16, 2012 9:39 pm
by rph
Missed a bracket is all. Should work now.
Re: Foreach loop echo once
Posted: Fri Mar 16, 2012 11:00 pm
by squtternutbosh
Nearly there but not quite, now i only get the one "click for larger image" (as required)
but only one image is loaded to the fancy box - currently it loads all three (so you can scroll left or right to see additional ones within the fancybox viewer as standard)
but also displays "click here for larger image" multiple times for each additional image (see 1st image on original post).
All i can think is maybe there's a way of setting style="display:none" on the recurring "click here' "s but so far each way i do it achieves the same as your attempt...
Is a shame cos it's taken me months to make the additional images zoomable as my php knowledge is pretty poor!
Do appreciate you taking the time to help though Ryan, thanks

Re: Foreach loop echo once
Posted: Fri Mar 23, 2012 1:11 am
by squtternutbosh
Didn't think this would be quite so tricky...
Re: Foreach loop echo once
Posted: Fri Mar 30, 2012 4:59 pm
by squtternutbosh
Any other suggestions?
Re: Foreach loop echo once
Posted: Tue Nov 13, 2012 1:17 am
by squtternutbosh
Ha ha! Finally figured it out, only took 8 months!!
Code: Select all
<?php if ($images) { ?>
<div class="image-additional">
<?php foreach ($images as $image) { ?>
<a href="<?php echo $image['popup']; ?>" title="<?php echo $heading_title; ?>" class="cloud-zoom-gallery" rel="useZoom: 'zoom1', smallImage: '<?php echo $image['thumb']; ?>' ">
<img src="<?php echo $image['thumb']; ?>" width="74" height="74">
</a>
<?php } ?>
<br>
<?php $counter = 0; { ?>
<?php foreach ($images as $image) {
if ($counter==0) { ?>
<a href="<?php echo $image['popup']; ?>" title="<?php echo $heading_title; ?>" class="fancybox" rel="fancybox">Click here to view large images</a>
<?php } else { ?>
<a href="<?php echo $image['popup']; ?>" title="<?php echo $heading_title; ?>" class="fancybox" rel="fancybox"> </a>
<?php } $counter++ ; } ?>
<?php } ?>
</div>
<?php } ?>
As you can see code looks pretty ropey but it actually works, hope this helps anyone stuck with a similar problem - notice the repeated lines are there as blank spaces ' ' - this is probably not the soundest way to achieve this but am content and just happy to have worked it out through trial and error.
link here to the same page as the original post showing it working:
http://oceanlifestyle.com/shop/Gone-signs