我的数据库中有一个表,有类似的数据,但其他的有更多的数据。
我想要的是通过循环回波数据。我的问题是我想从2到2获取数据。
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i++) : ?>
<div class="item">
<?php echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;?>
</div>
<?php endfor; ?>
</div>这会返回
<div class="owl-4">
<div class="item">
<h3>Author 1</h3>
<p>Testimony 1</p>
</div>
<div class="item">
<h3>Author 2</h3>
<p>Testimony 2</p>
</div>
................
</div>我怎么才能把这个给我
<div class="owl-4">
<div class="item">
<h3>Author 1</h3>
<p>Testimony 1</p>
<h3>Author 2</h3>
<p>Testimony 2</p>
</div>
<div class="item">
<h3>Author 3</h3>
<p>Testimony 3</p>
<h3>Author 4</h3>
<p>Testimony 4</p>
</div>
................
</div>谢谢
发布于 2015-12-03 15:42:49
每次只需增加2台,而不是1台。
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i += 2) : ?>
<div class="item">
<?php
echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;
echo '<h3>'.$helper->get('authorname', $i + 1).'</h3>';
echo '<p>'.$helper->get('testimony', $i + 1).'</p>' ;
?>
</div>
<?php endfor; ?>
</div>发布于 2015-12-03 15:42:29
您可以每两个迭代显示<div class="item">和</div>,例如检查$i是否为偶数:
<?php $count = $helper->count('testimony');?>
<div class="owl-4">
<?php for ($i=0; $i < $count; $i++) : ?>
<?php if ($i%2==0): ?>
<div class="item">
<?php endif; ?>
<?php echo '<h3>'.$helper->get('authorname', $i).'</h3>';
echo '<p>'.$helper->get('testimony', $i).'</p>' ;?>
<?php if ($i%2==0): ?>
</div>
<?php endif; ?>
<?php endfor; ?>
https://stackoverflow.com/questions/34069942
复制相似问题