tzflorida Posted June 22, 2008 Share Posted June 22, 2008 I would like to give thumbnails in a gallery alternating CSS classes. In other words, I would like to give the first thumbnail the “.mycssclass1”, the second thumbnail “.mycssclass2”, and then repeat in that order, always alternating between the two classes. Here is the code I am working with: <?php foreach ($this->getGalleryImages() as $_image): ?> <li> <a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'scrollbars=yes,width=300,height=300,resizable=yes');return false;"> <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->directResize(140, 300, 1); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/> </a> </li> <?php endforeach; ?> The reason I need such function is to float half of the thumbnails to the left and half to the right. I really appreciate any help in this. P.S. Sorry if the title of this post does not make sense, but as I am a layman when it comes to PHP, I did not quite know how to express this problem. Quote Link to comment Share on other sites More sharing options...
darkhappy Posted June 22, 2008 Share Posted June 22, 2008 create a counter for the loop, then put an if statement inside the loop to check if the counter is even or odd. if its even apply class 1, if its odd apply class 2. there are several ways to check if an integer is even or odd, i am not sure the best way but there are some examples here: http://www.php.net/is_numeric. <?php $counter = 0 foreach ($array as $value) { $counter = counter + 1; if (counter is even) { class = 1; } else { class = 2; } //rest of foreach loop }?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Uhh...Huh? if (counter is even) { ? That wouldn't work. Use: if ($counter % 2 == 0) { $class = 1; } else { $class=2; } $counter++; Quote Link to comment Share on other sites More sharing options...
darkhappy Posted June 23, 2008 Share Posted June 23, 2008 Uhh...Huh? if (counter is even) { ? That wouldn't work. Use: if ($counter % 2 == 0) { $class = 1; } else { $class=2; } $counter++; obviously - this is why i linked the php.net article for that piece, thanks for filling it in. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 That PHP link had nothing to do with this. :-\ Quote Link to comment Share on other sites More sharing options...
tzflorida Posted June 23, 2008 Author Share Posted June 23, 2008 Thanks for the help. So I tried implementing it however, I seem to be running into the following error message: Parse error: syntax error, unexpected T_FOREACH This is how I implemented the piece of code: <?php $_product = $this->getProduct() ?> <?php if (count($this->getGalleryImages()) > 0): ?> <div class="more-views"> <h4><?php echo $this->__('Sample Images') ?></h4> <ul> <?php $counter = 0 foreach ($array as $value) { $counter = counter + 1; if ($counter % 2 == 0) { $class = 1; } else { $class=2; } $counter++; }?> <?php foreach ($this->getGalleryImages() as $_image): ?> <li> <a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'scrollbars=yes,width=300,height=300,resizable=yes');return false;"> <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->directResize(140, 300, 1); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/> </a> </li> <?php endforeach; ?> </ul> </div> <?php endif; ?> Did I put it in the right place? I am not at all familiar with PHP. Thanks Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Firstly, you missed a semicolon ( after you definer $counter, which gives you your error. Secondly, that goes inside your current foreach loop and you drop the declaration for a new one. Quote Link to comment Share on other sites More sharing options...
tzflorida Posted June 23, 2008 Author Share Posted June 23, 2008 I feel stupid ??? I added the semicolon (that was the easy part) but I think I did not correctly insert the code as you specified. Here is what I did: <?php $_product = $this->getProduct() ?> <?php if (count($this->getGalleryImages()) > 0): ?> <div class="more-views"> <h4><?php echo $this->__('Sample Images') ?></h4> <ul> <?php $counter = 0; foreach ($this->getGalleryImages() as $_image) { $counter = counter + 1; if ($counter % 2 == 0) { $class = 1; } else { $class=2; } $counter++; } ?> <li> <a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'scrollbars=yes,width=300,height=300,resizable=yes');return false;"> <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->directResize(140, 300, 1); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>"/> </a> </li> <?php endforeach; ?> </ul> </div> <?php endif; ?> Thanks Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Pretty close, you just need to echo the class out somewhere in the tag. echo 'class="myclass' . $class . '"'; Quote Link to comment Share on other sites More sharing options...
tzflorida Posted June 23, 2008 Author Share Posted June 23, 2008 I added that to the image tag but I get the following error message: Parse error: syntax error, unexpected T_ENDFOREACH Here is the current code: <?php $_product = $this->getProduct() ?> <?php if (count($this->getGalleryImages()) > 0): ?> <div class="more-views"> <h4><?php echo $this->__('Sample Images') ?></h4> <ul> <?php $counter = 0; foreach ($this->getGalleryImages() as $_image) { $counter = counter + 1; if ($counter % 2 == 0) { $class =1; } else { $class=2; } $counter++; } ?> <li><a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image) ?>', 'gallery', 'scrollbars=yes,width=300,height=300,resizable=yes');return false;"> <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->directResize(140, 300, 1); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" title="<?php echo $this->htmlEscape($_image->getLabel()) ?>" echo 'class="myclass' . $class . '"';/></a> </li><?php endforeach; ?></ul></div><?php endif; ?> I really hope this can be done Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Replace: <?php endforeach; ?> With: <?php } ?> Quote Link to comment Share on other sites More sharing options...
tzflorida Posted June 23, 2008 Author Share Posted June 23, 2008 That gives me: Parse error: syntax error, unexpected '}' in Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Oh. You didn't enclose the echo in PHP tags. <?php echo 'class="myclass' . $class . '"' ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Also, on the rest of the echos inside the HTML block, you missed the ; on the end of the lines. Fix those. Quote Link to comment Share on other sites More sharing options...
tzflorida Posted June 23, 2008 Author Share Posted June 23, 2008 Am still getting: Parse error: syntax error, unexpected '}' This is the current code: <?php $_product = $this->getProduct() ?> <?php if (count($this->getGalleryImages()) > 0): ?> <div class="more-views"> <h4><?php echo $this->__('Sample Images') ?></h4> <ul> <?php $counter = 0; foreach ($this->getGalleryImages() as $_image) { $counter = counter + 1; if ($counter % 2 == 0) { $class =1; } else { $class=2; } $counter++; } ?> <li><a href="#" onclick="popWin('<?php echo $this->getGalleryUrl($_image); ?>', 'gallery', 'scrollbars=yes,width=300,height=300,resizable=yes');return false;"> <img src="<?php echo $this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())->directResize(140, 300, 1); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel()); ?>" title="<?php echo $this->htmlEscape($_image->getLabel()); ?>" <?php echo 'class="myclass' . $class . '"'; ?> /></a> </li><?php } ?></ul></div><?php endif; ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Change: } $counter++; } ?> To: } $counter++; ?> Quote Link to comment Share on other sites More sharing options...
darkhappy Posted June 23, 2008 Share Posted June 23, 2008 That PHP link had nothing to do with this. :-\ the syntax you used was in the link i provided in my original response, that you claim has nothing to do with this. read first, then second-guess - get it? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Yeah, because someone posted it...but the page doesn't necessarily mention it....And if you knew the code was there, why didn't you just post it? =/ Quote Link to comment Share on other sites More sharing options...
darkhappy Posted June 23, 2008 Share Posted June 23, 2008 Yeah, because someone posted it...but the page doesn't necessarily mention it....And if you knew the code was there, why didn't you just post it? =/ ...ZZZZz....... Quote Link to comment Share on other sites More sharing options...
tzflorida Posted June 23, 2008 Author Share Posted June 23, 2008 Now I get this error message: Notice: Use of undefined constant counter - assumed 'counter' Thanks Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Remove this line, idk why it was in there: $counter = counter + 1; Quote Link to comment Share on other sites More sharing options...
tzflorida Posted June 23, 2008 Author Share Posted June 23, 2008 Remove this line, idk why it was in there: $counter = counter + 1; That line was there from the initial code that darkhappy suggested. But now it's working like a charm... yeah Anyhow, I really appreciate your help. You really know what you are doing. Now I know who to go to personally for any future PHP problems Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Heh, alright, glad I could help. =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.