0

下面的这个脚本在不同的生产线上输出产品的所有规格(在白色和灰色之间切换)。

我希望脚本以 2 x 2 的形式输出规格。

所以而不是:

label - value 
label - value 
label - value

我想:

label - value    label - value 
label - value    label - value 
label - value    label - value

有谁知道我应该如何实现这一目标?

当前代码是:

<?php
$color='grey';
$attributes = $_product->getAttributes();
foreach ($attributes as $attribute) {
    if ($attribute->getIsVisibleOnFront() and $attribute->getFrontend()->getValue($_product)) 
    {
    ?>
        <div class="row-<?php $color=($color=='grey')? 'white' : 'grey'; echo $color; ?>">
            <div class="name-detail">
                <?php echo $attribute->getFrontend()->getLabel($_product); ?>
            </div>
            <div class="description">
                <?php echo $attribute->getFrontend()->getValue($_product); ?>
            </div>
        </div>
    <?php
    }
}
?>
4

1 回答 1

0

只要有一个柜台告诉你,如果你在%2或没有。

这应该有效(未经测试,但足以说明这里的意思):

<?php
$color='grey';
$i = 0;
$attributes = $_product->getAttributes();
foreach ($attributes as $attribute) {
    if ($attribute->getIsVisibleOnFront() and $attribute->getFrontend()->getValue($_product)) 
    {
        if ($i % 2)
        {
?>
        <div class="row-<?php $color=($color=='grey')? 'white' : 'grey'; echo $color; ?>">
<?php
        }
?> 
            <div class="name-detail">
                <?php echo $attribute->getFrontend()->getLabel($_product); ?>
            </div>
            <div class="description">
                <?php echo $attribute->getFrontend()->getValue($_product); ?>
            </div>
<?php
        if (($i + 1) % 2)
        {
?>
        </div>
<?php
        }
        $i++;
    }
}
if (($i + 1) % 2)
{
?>
    </div>
<?php
}
?>

我试图在这里坚持你的编码风格。

如您所知,您可以使用它echo来输出 html,它会以这种方式给出一些东西:

<?php
  $color='grey';
  $i = 0;
  $attributes = $_product->getAttributes();
  foreach ($attributes as $attribute) {
    if ($attribute->getIsVisibleOnFront() && $attribute->getFrontend()->getValue($_product)) {
      if ($i % 2)
        echo '<div class="row-' . (($color == 'grey') ? 'grey' : 'white') . '">';
      echo '<div class="name-detail">' . $attribute->getFrontend()->getLabel($_product) . '</div>';
      echo '<div class="description">' . $attribute->getFrontend()->getValue($_product) . '</div>';
      if (($i + 1) % 2)
        echo '</div>';
      $i++;
    }
  }
  if (($i + 1) % 2)
    echo '</div>';
?>
于 2013-11-12T21:08:59.110 回答