0

I'm creating a layout override for the category layout where I need to list the articles in a 2 column table:

+-----------+------------+
|           |            |
|   item 1  |   item 2   |
|           |            |
+-----------+------------+
|           |            |
|   item 3  |   item 4   |
|           |            |
+-----------+------------+
        ...etc...

but the articles are showing up duplicated:

+-----------+------------+
|           |            |
|   item 1  |   item 1   |
|           |            |
+-----------+------------+
|           |            |
|   item 2  |   item 2   |
|           |            |
+-----------+------------+
        ...etc...

I'm not sure how to increment the item# in the loop to make it show 2 items per row. here is the code I'm dealing with:

<?php if (!empty($this->intro_items)) : ?>
                <table cellpadding="0" cellspacing="0" width="600" border="0" style="width: 580px !important;">
                    <?php foreach ($this->intro_items as $key => &$item) : ?>
                    <?php
                        $key= ($key-$leadingcount)+1;
                        $rowcount=( ((int)$key-1) % (int) $this->columns) +1;
                        $row = $counter / $this->columns ;

                        if ($rowcount==1) : ?>
                    <?php endif; ?>
                    <tr>
                        <td class="item column-<?php echo $rowcount;?><?php echo $item->state == 0 ? ' system-unpublished' : null; ?>">
                        <?php
                            $this->item = &$item;
                            echo $this->loadTemplate('item');
                        ?>
                        </td>
                        <td width="20">&nbsp;</td>
                        <td class="item column-<?php echo $rowcount;?><?php echo $item->state == 0 ? ' system-unpublished' : null; ?>">
                        <?php
                            $this->item = &$item;
                            echo $this->loadTemplate('item');
                        ?>
                        </td>
                    </tr>
                        <?php if (($rowcount == $this->columns) or ($counter ==$introcount)): ?>

                        <?php endif; ?>
                    <?php endforeach; ?>
                </table>

            <?php endif; ?>
4

You can't create two cells the same time within a foreach loop. Change your code as below,

<?php if (!empty($this->intro_items)) : ?>
    <table cellpadding="0" cellspacing="0" width="600" border="0" style="width: 580px !important;">
        <?php $count = 0; ?>
        <?php foreach ($this->intro_items as $key => &$item) : ?>
            <?php if ($count%2 == 0) : ?>
            <tr>            
            <?php endif; ?>
                <td class="item column-<?php echo $rowcount;?><?php echo $item->state == 0 ? ' system-unpublished' : null; ?>">
                <?php
                    $this->item = &$item;
                    echo $this->loadTemplate('item');
                ?>
                </td>
            <?php if ($count%2 == 0) : ?>
            </tr>           
            <?php endif; ?>
            <?php $count++; ?>
        <?php endforeach; ?>
    </table>
<?php endif; ?>
  • This is good, but I had to move the $count++ before the </tr> tag. Thanks! – lbweb Oct 5 '15 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.