PHP为每三组数据创建一行

时间:2015-10-30 12:39:08

标签: php loops foreach

我正在处理这个项目,我需要在菜单中加载不同的项目。我的想法是使用foreach循环来完成它,这可以使我获得项目,但我无法弄清楚如何为每3个项目创建行。

HTML

     <div class="row box-2">
                <div class="grid_4">
                    <div class="img"><div class="lazy-img" style="padding-bottom: 76.21621621621622%;"><img data-src="images/page-4_img07.jpg" alt=""></div></div>
                    <h3>Anteger convallis orci vel mi nelaoreet, at ornare lorem consequat. </h3>
                    <p>Vestibulum volutpatturpis ut massa commodo, quis aliquam massa facilisis.Integer convavel miberto merlonelaoreet, at ornare lorem consequat.</p>
                    <a href="#" class="btn">Read more</a>
                </div>
                <div class="grid_4">
                    <div class="img"><div class="lazy-img" style="padding-bottom: 76.21621621621622%;"><img data-src="images/page-4_img08.jpg" alt=""></div></div>
                    <h3>Genteger convallis orci vel mi nelaoreet, at ornare lorem consequat.</h3>
                    <p>Meestibulum volutpatturpis ut massa commodo, quis aliquam massa facilisis.Integer convavel miberto merlonelaoreet, at ornare lorem consequatre. </p>
                    <a href="#" class="btn">Read more</a>
                </div>
                <div class="grid_4">
                    <div class="img"><div class="lazy-img" style="padding-bottom: 76.21621621621622%;"><img data-src="images/page-4_img09.jpg" alt=""></div></div>
                    <h3>Ternteger convallis orci vel mi nelaoreet, at ornare lorem consequat. </h3>
                    <p>Testibulum volutpatturpis ut massa commodo, quis aliquam massa facilisis.Integer convavel miberto merlonelaoreet, at ornare lorem consequ.</p>
                    <a href="#" class="btn">Read more</a>
                </div>
            </div>

PHP

<?php 
            foreach ($menuItems as $dish => $item) { 
            ?>

        <div class="grid_4">
            <div class="img"><div class="lazy-img" style="padding-bottom: 76.21621621621622%;"><img data-src="images/<?php echo $item[img];?>.jpg" alt="<?php echo $item[name]; ?>"></div></div>
            <h3><?php echo $item[title]; ?></h3>
            <a href="dish.php?item=<?php echo $dish; ?>" class="btn">Read more about <?php echo $item[title]; ?></a>
        </div>
        <?php 
        } ?>

2 个答案:

答案 0 :(得分:0)

   <?php $i = 1; ?>
   <?php 
        foreach ($menuItems as $dish => $item) { 
        ?>
    <?php if($i % 3 == 0 || $i == 1) { ?>
         <div class="row box">
    <?php } ?>
    <div class="grid_4">
        <div class="img"><div class="lazy-img" style="padding-bottom: 76.21621621621622%;"><img data-src="images/<?php echo $item[img];?>.jpg" alt="<?php echo $item[name]; ?>"></div></div>
        <h3><?php echo $item[title]; ?></h3>
        <a href="dish.php?item=<?php echo $dish; ?>" class="btn">Read more about <?php echo $item[title]; ?></a>
    </div>
    <?php if($i % 3 == 0 || $i == 1) { ?>
         </div>
    <?php } ?>
    <?php $i++; ?>
    <?php 
    } ?>

答案 1 :(得分:0)

So if I am not mistaken, for example if you have 7 items you get 3 rows. 2 rows with 3 items and 1 row with 1 item? Perhaps this setup can help you with your menu: <?php $itemsForRow = 3; $itemTemplate = <<<'TEMPLATE' <div class="img"><div class="lazy-img" style="padding-bottom: 76.21621621621622%;"><img data-src="images/%2$s.jpg" alt="%1$s"></div></div> <h3>%1$s</h3> <a href="dish.php?item=%3$s" class="btn">Read more about %1$s</a> TEMPLATE; if (count($menuItems) > 0) { $itemCounter = 0; foreach ($menuItems as $dish => $item) { if ($itemsForRow > 0 && $itemCounter % $itemsForRow === 0) { echo '<div class="grid_4">'; } echo sprintf( $itemTemplate, $item["name"], $item["title"], $item["img"], $dish ); if ($itemsForRow > 0 && ($itemCounter % $itemsForRow === ($itemsForRow - 1) || $itemCounter == count($menuItems) - 1)) { echo '</div>'; } $itemCounter++; } } ?>