这个foreach循环应该循环12次,但只循环4.为什么?我究竟做错了什么?我试图将产品图像放入滑动的carosel中。前4个图像看起来没有问题,但其他8个图像根本不会出现。
<div class="container">
<div class="col-md-12">
<h1>In the Spotlight</h1>
<div class="well">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<?php
foreach ($products as $key => $product) { ?>
<div class="item <?php echo ($key == 0?"active":"");?>">
<?php if ($key == 0)
{
echo "<div class=\"row\">";
}
elseif ($key == 4)
{
echo "<div class=\"row\">";
}
elseif ($key == 8)
{
echo "<div class=\"row\">";
}
else
{
}
?>
<?php if ($product['thumb']) { ?>
<div class="col-md-3"><a class="thumbnail" href="<?php echo $product['href']; ?>"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" style="max-width: 100%;" /></a></div>
<?php } ?>
<?php if ($key == 3)
{
echo "</div></div>";
}
elseif ($key == 7)
{
echo "</div></div>";
}
elseif ($key == 11)
{
echo "</div></div>";
}
else
{
}
?>
<?php } ?>
</div>
</div>
<!--/carousel-inner--> <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
</div>
<!--/myCarousel-->
</div>
<!--/well-->
</div>
</div>
SOURCE OUTPUT
<div class="container">
<div class="col-md-12">
<h1>In the Spotlight</h1>
<div class="well">
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<!-- Carousel items -->
<div class="carousel-inner">
<div class="item active">
<div class="row">
<div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&product_id=43"><img src="http://localhost/theme/image/cache/data/demo/macbook_1-250x250.jpg" alt="MacBook" style="max-width: 100%;" /></a></div>
<div class="item ">
<div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&product_id=40"><img src="http://localhost/theme/image/cache/data/demo/iphone_1-250x250.jpg" alt="iPhone" style="max-width: 100%;" /></a></div>
<div class="item ">
<div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&product_id=42"><img src="http://localhost/theme/image/cache/data/demo/w1-250x250.jpg" alt="Apple Cinema 30"" style="max-width: 100%;" /></a></div>
<div class="item ">
<div class="col-md-3"><a class="thumbnail" href="http://localhost/theme/index.php?route=product/product&product_id=49"><img src="http://localhost/theme/image/cache/data/demo/samsung_tab_1-250x250.jpg" alt="Samsung Galaxy Tab 10.1" style="max-width: 100%;" /></a></div>
</div></div>
</div>
</div>
<!--/carousel-inner--> <a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
</div>
<!--/myCarousel-->
</div>
<!--/well-->
</div>
</div>
print_r($ products)显示:
Array ( [0] => Array ( [product_id] => 43 [thumb] => http://localhost/theme/image/cache/data/demo/macbook_1-250x250.jpg [name] => MacBook [price] => £500.00 [special] => [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=43 ) [1] => Array ( [product_id] => 40 [thumb] => http://localhost/theme/image/cache/data/demo/iphone_1-250x250.jpg [name] => iPhone [price] => £101.00 [special] => [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=40 ) [2] => Array ( [product_id] => 42 [thumb] => http://localhost/theme/image/cache/data/demo/w1-250x250.jpg [name] => Apple Cinema 30" [price] => £100.00 [special] => £90.00 [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=42 ) [3] => Array ( [product_id] => 49 [thumb] => http://localhost/theme/image/cache/data/demo/samsung_tab_1-250x250.jpg [name] => Samsung Galaxy Tab 10.1 [price] => £199.99 [special] => [rating] => 0 [reviews] => Based on 0 reviews. [href] => http://localhost/theme/index.php?route=product/product&product_id=49 ) )
提前致谢
答案 0 :(得分:1)
foreach
总是循环遍历数组,因此:您的数组有4个项目,这就是foreach
进行4次迭代的原因。在这种情况下,foreach
类似于:
for ($i = 0, $c = count($your_array); $i < $c; $i++)
那就是说,你不应该期望foreach
迭代次数超过数组中的项目。