如何在三列的行中划分类别

时间:2012-02-29 10:59:53

标签: php magento

我有一个foreach循环,它检索当前类别的子类别。

<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<div class="item">
    <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?></a>
</div>
<?php endforeach; ?>

我想将结果分成三列的行。然后是所需的格式:

<div class="row">
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
</div>
<div class="row">
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
</div>
...

--------------- EDIT ----------------

我的代码现在如下。我如何以及在哪里关闭行div?

<?php $_categories = $this->getCurrentChildCategories(); ?>
<?php foreach ($_categories as $_category): ?>
<?php if ($i % 3 == 0) { ?>
<div class="row">
<?php } ?>
<div class="item">
    <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> &raquo;</a></h2>
</div>
<?php $i++; endforeach; ?>

---------------编辑2 ----------------

越来越近了。这是结果:

<div class="category-list">
    <div class="row"></div>
<div class="row">
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
</div>
<div class="row">
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
</div>
<div class="row">
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
    <div class="item">
        content
    </div>
</div>
</div>

如何摆脱空行?

现在的代码如下:

<div class="category-list">

<?php
$i=0;
echo '<div class="row">';
$_categories = $this->getCurrentChildCategories();
foreach($_categories as $_category):
{
if($i %3 ==0)
  { ?>
     </div>
     <div class="row">
        <div class="item">
            <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> &raquo;</a></h2>
        </div>
  <? }
  else
  { ?>
        <div class="item">
            <h2><a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>"><?php echo $this->htmlEscape($_category->getName()) ?> &raquo;</a></h2>
        </div> 
  <? }
  $i++;
}
endforeach;
?>

</div>

---------------编辑3 ----------------

通过CSS隐藏空行来解决。

2 个答案:

答案 0 :(得分:1)

您必须引入az迭代器(?)变量,例如$ i = 0

if ($i % 3 == 0) { //modulo
   // use for whatever you want
}

在每个周期(可能在它的最后,它取决于你的真实解决方案)你必须增加$ i(++ $ i);

答案 1 :(得分:1)

希望这有帮助

<?php
$i=0;
echo"<div class="row" >
foreach($category as $cat)
{
if($i %3 ==0)
  {
     echo"</div><div class='row'> <div class='item'> Content </div>";  
  }
  else
  {
    echo"<div class='item'> Content </div>";  
  }
  $i++;
}
echo"</div>";
?>