【问题标题】:How can I alternate the column count per row?如何交替每行的列数?
【发布时间】:2015-09-24 04:33:53
【问题描述】:

我有一个包含大约 90 个项目的 PHP 对象。我试图用交替列输出这些行。我当前的代码每行输出 2 项是:

<?php 
    $_collectionSize = $_productCollection->count();
    $_columnCount = 2;
    $i = 0;
?>

<?php foreach ($_productCollection as $_product): ?>

    <?php if ($i++ % $_columnCount == 0): ?>
        <section class="row">
    <?php endif ?>

            <div class="six columns"></div>

    <?php if ($i % $_columnCount == 0 || $i == $_collectionSize): ?>
        </section>
    <?php endif ?>

<?php endforeach; ?>

如何修改此代码以交替每行的列数,以便输出如下:

<div class="row">
    <div class="six columns"></div>
    <div class="six columns"></div>
</div>

<div class="row">
    <div class="three columns"></div> 
    <div class="three columns"></div>
    <div class="three columns"></div>
    <div class="three columns"></div>
</div>

<div class="row">
    <div class="six columns"></div>
    <div class="six columns"></div>
</div>

<div class="row">
    <div class="three columns"></div> 
    <div class="three columns"></div>
    <div class="three columns"></div>
    <div class="three columns"></div>
</div>

谢谢

【问题讨论】:

  • Google for PHP 模板引擎(如 Smarty 或 Twig)。你以后可以感谢我;)

标签: php object foreach


【解决方案1】:

我会将我的数组分成两块,然后保存下一个需要的键以输出不同的标记:

$items = array(
    'Product 1',
    'Product 2',
    'Product 3',
    'Product 4',
    'Product 5',
    'Product 6',
    'Product 7',
    'Product 8',
    'Product 9',
    'Product 10',
    'Product 11',
    'Product 12',    
);

$chunked = array_chunk($items, 2);

// variable to hold next <div class="six columns"></div> markup
$needle = 0;

foreach ($chunked as $key => $items) {

    if ($key == $needle) {
        if ($key !== 0) echo "</div>\n";
        echo "<div class=\"row\">\n";
        foreach($items as $item) {
            echo "<div class=\"six columns\">{$item}</div>\n";
        }
        echo "</div>\n<div class=\"row\">\n";
        // skip two array items
        $needle = $needle + 3;
    } else {
        foreach($items as $item) {
            echo "<div class=\"three columns\">{$item}</div>\n";
        }
    }
}
echo "</div>";

Working demo

【讨论】:

    【解决方案2】:

    你的意思是这样的,使用 2 的模数? :

    <?php foreach ($_productCollection as $_product): ?>
    
        <?php if ($i++ % $_columnCount == 0): ?>
            <section class="row">
        <?php endif ?>
    
        <?php if ($i % 2 == 0): ?>
                <div class="six columns"></div>
                <div class="six columns"></div>
        <?php else ?>
                <div class="three columns"></div> 
                <div class="three columns"></div>
                <div class="three columns"></div>
                <div class="three columns"></div>
        <?php endif ?>
    
        <?php if ($i % $_columnCount == 0 || $i == $_collectionSize): ?>
            </section>
        <?php endif ?>
    
    <?php endforeach; ?>
    

    【讨论】:

    • 感谢您的回复,但这不是我想要的。每行输出 3、3、3、3、6、6。我需要一排 6、6,然后是 3、3、3、3 交替
    猜你喜欢
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 2017-07-06
    • 2019-09-12
    相关资源
    最近更新 更多