我有一个猫头鹰旋转木马,每张幻灯片显示两张推荐书。但是这两张幻灯片位于我要循环的1个元素内。每个推荐都来自数据库。
所以例如我得到了这样的循环:
foreach($array as $testimonial){
$testimonials .= '
<div class="testwrapper">
<div class="testimonial">
'.$testimonial['title'].'
</div>
<div class="testimonial">
'.$testimonial['title'].'
</div>
</div>';
}
这两个标题都来自不同的推荐,我想我必须在数组中使用它们的数字,但我怎么能这样做呢?因此,第一张幻灯片上的数组值为0,1,然后是第二张幻灯片上的2,3。
RichardBernards回答后的当前代码:
// Haal alle referenties/testimonials op
$test = "SELECT * FROM `web_content` WHERE catid = 12 AND state = 1 ORDER BY ordering";
$testcon = $conn->query($test);
$testcr = array();
while ($testcr[] = $testcon->fetch_array());
for($i = 0; $i <= count($testcr); $i+2) {
$testimonials .= '
<div class="reviews">
<div class="client left">
<p class="large comment">
'.$testcr[$i]['title'].'
</p>
<div class="media">
<div class="media-left">
<a href="#fakelink">
<img class="avatar media-object " src="assets/images/klanten/sdgsdg.png" alt="avatar" />
</a>
</div>
<div class="media-body">
<h5 class="media-heading">Name</h5>
<p>Bedrijf</p>
</div>
</div>
</div>
<div class="client right">
<p class="large comment">
'.$testcr[$i+1]['title'].'
</p>
<div class="media">
<div class="media-left">
<a href="#fakelink">
<img class="avatar media-object " src="assets/images/klanten/testimonial_foto_sdgdsg.jpg" alt="avatar" />
</a>
</div>
<div class="media-body">
<h5 class="media-heading">Name</h5>
<p>Bedrijf</p>
</div>
</div>
</div>
</div>';
}
echo $testimonials;
答案 0 :(得分:1)
像这样......使用for循环:
for($i = 0; $i <= count($array); $i+=2) {
$testimonials .= '
<div class="testwrapper">
<div class="testimonial">
'.$array[$i]['title'].'
</div>
<div class="testimonial">
'.$array[$i+1]['title'].'
</div>
</div>';
}