2

My current code:

<?php if($_images){?>           
    <?php $i=0; foreach($_images as $_image){ $i++; ?>
        <?php if($i > 2) { ?>
            <div class = "largePic">
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
            </div>
        <?php } else { ?>
            <div class = "smallPic">
            <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
            </div>
        <?php } ?>

    <?php } ?>  
<?php } ?>  

So this is obviously wrong since every time an image is echo'ed it's assigned to a different div (same name but different). Is it possible to assign echoes to certain div depending on the count?

For example, first two images will be assigned to the smallPic div, then the rest will be in the largePic div.

5
  • And what is wrong now? Commented Jan 4, 2017 at 15:19
  • 1
    Just put the divs outside the foreach and use 2 foreach instead (removing pictures in the $_images array when its assigned to a div). Commented Jan 4, 2017 at 15:19
  • The problem now is that the first two images are read as two different divs so they line up on top of each other instead of right next to each other. @u_mulder Commented Jan 4, 2017 at 15:21
  • Sounds like that is more of a styling issue than a PHP issue. Why not use CSS to make them appear next to each other? Commented Jan 4, 2017 at 15:22
  • I could but it's still pretty messy seeing a bunch of divs under the same name occupying the space. If it's possible I want them to be group under one div. Commented Jan 4, 2017 at 15:25

3 Answers 3

2

Process the images first (storing the HTML for each image in either an array or string) and then create the div elements- see example below. This will reduce the number of opening/closing tags immensely.

Also note that if the keys of the array are numeric then the following syntax can be used instead of creating an extra variable (i.e. $i) just to track the index (so as to avoid extra bookkeeping like incrementing the variable - one of the major benefits of foreach compared to a for statement).

foreach (array_expression as $key => $value)
      statement 1

<?php 
$largePic = '';
$smallPic = '';
if($_images){      
    foreach($_images as $i => $_image){ 
        if($i > 2) { 
           $largePic .= '<img src="'.$this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900).'" alt="'. $this->htmlEscape($_image->getLabel()). '" title="'. $this->htmlEscape($_image->getLabel()).'" />';
         } else { 
            $smallPic .= '<img src="'. $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675). '" alt="'. $this->htmlEscape($_image->getLabel()). '" title="'. $this->htmlEscape($_image->getLabel()). '" />';
        } 
    }  
 } ?> 
<div class = "largePic"><?php echo $largePic; ?></div>  
<div class = "smallPic"><?php echo $smallPic; ?></div>
Sign up to request clarification or add additional context in comments.

Comments

2

What you want is to first devide your array into two arrays and then foreach through both of them.

<?php

$largeImages = array();
$smallImages = array();

foreach ($_images as $k => $v) {
    if ($k > 2) {
        $largeImages[] = $v;
    } else {
        $smallImages[] = $v
    }
}
?>
<div class = "largePic">
<?php foreach ($largeImages as $_image) { ?>
    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
</div>
<div class = "smallPic">
<?php foreach ($smallImages as $_image) { ?>
    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
</div>

Comments

0

I'm not in PHP but I'm quite sure for start you can loose those extensive open/close tags (<?php ?>) and replace it with only one at the start and the end. This is hardly readable.

Then, like in many server side scripts, what you need is some kind of buffer containing HTML which you will output at the end of loop. Because what you're doing now is outputting from inside the loop, this sending multiple DIVs to client.

Create largePic nad smallPic variables contianing DIVs HTML, and append HTML to it inside loop (instead outputing it to client like you do now). Then, after loop is finished, output those two variables to client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.