Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 脚本输出为2列,而不是1列_Php_Html_Mysql - Fatal编程技术网

Php 脚本输出为2列,而不是1列

Php 脚本输出为2列,而不是1列,php,html,mysql,Php,Html,Mysql,下面的脚本在不同的行上输出产品的所有规格(在白色和灰色之间切换) 我希望脚本以2乘2的方式输出规范 因此,不是: label - value label - value label - value 我想: label - value label - value label - value label - value label - value label - value 有人知道我应该如何做到这一点吗 目前的代码是: <?php $color='grey';

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

我希望脚本以2乘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
    }
}
?>


只需让一个计数器告诉您是否在
%2

这应该有效(未经测试,但足以说明问题):


改为使用
for
循环。然后,您可以根据
$i
2
的倍数来配置列数。非常感谢您的快速帮助。在您的解决方案中,输出第一个变量并设置样式,输出其他值时不设置样式。所以它看起来像:label-value(styled)label-value label-value…等等谢谢你的帮助!我已经在这里编辑了失败,我想这是最后一次,如果没有正确放置。您好。它在“$i++”行失败,错误为:Parse error:syntax error,意外的T_变量,应为“,”或“;”在%path%Sorrie!希望你有另一个好主意!认真地你能调试一下丢失的分号吗?我觉得自己很愚蠢;-)但是缺少的分号是什么?它应该在哪里?
<?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
}
?>
<?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>';
?>