如何显示 2 in 2 循环中的数据 php
how to display data from 2 in 2 loop php
我的数据库中有一个 table 具有相似的数据,但其他的具有更多数据。
我想要的是通过循环回显数据。我的问题是我想从 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>
这个returns
<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>
谢谢
您可以每两次迭代显示<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; ?>
只需将计数器每次增加 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>
我的数据库中有一个 table 具有相似的数据,但其他的具有更多数据。
我想要的是通过循环回显数据。我的问题是我想从 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>
这个returns
<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>
谢谢
您可以每两次迭代显示<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; ?>
只需将计数器每次增加 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>