Jump to content

[SOLVED] PHP Call Alternating CSS Class


tzflorida

Recommended Posts

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.

Link to comment
Share on other sites

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

 

   

Link to comment
Share on other sites

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  :)

Link to comment
Share on other sites

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  :)

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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  8)

 

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  ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.