如何显示2 in 2循环php中的数据

时间:2015-12-03 15:33:25

标签: php for-loop modulus

我的数据库中有一个包含类似数据的表,但其他数据包含更多数据。

我想要的是通过循环回显数据。我的问题是我想从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>

感谢

2 个答案:

答案 0 :(得分:1)

您可以每两次迭代显示<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; ?>

答案 1 :(得分:1)

只需将计数器增加2而不是每次增加1。

<div class="panel panel-default">
                <div class="panel-body">This page is temporarily disabled by the site administrator for some reason.</div> 
                <div class="panel-footer clearfix">
                    <p>Drink whaterver jklasd jklasd jklnxm,c kasdk jj jjjjs lasd jklasd m,zxc asd kljaskd kljasd kl</p>
                    <div style="border: 1px solid black; display: block;" class="pull-left">
                        <div>
                            <p>By <a href="#">Jerald Patalinghug</a></p>
                        </div>
                        <div>
                            Tags: <a href="#">Funny</a>, <a href="#">Wtf</a>, <a href="#">Nice</a>
                        </div>
                    </div>
                    <div style="border: 1px solid black;" class="pull-right">
                        <div id="votes">
                            <div>
                                                            <a href="#" data-card_id="26" class="vote upvote btn btn-default">
                                    <span class="fa fa-thumbs-up"></span>
                                </a>
                                <a href="#" data-card_id="26" class="vote downvote btn btn-default">
                                    <span class="fa fa-thumbs-down"></span>
                                </a>
                                                        </div>
                            <div>
                                <center>
                                    <span class="vote_count">1</span> points
                                </center>
                            </div>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>