我正在处理这个项目,我需要在菜单中加载不同的项目。我的想法是使用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
} ?>
答案 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)