Leoalv
Leoalv

Reputation: 25

how to display data from 2 in 2 loop php

I have a table in my database with similar data, but others with more data.

What I want is to echo the data through the loop. my problem is that I want to get data from 2 to 2. My code to display data normally is

<?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>

This 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>

How do I turn gives me this

<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>

THANKS

Upvotes: 0

Views: 118

Answers (2)

Christian
Christian

Reputation: 1577

Just increase the counter by 2 instead of 1 each time.

<?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>

Upvotes: 1

Carlos Mayo
Carlos Mayo

Reputation: 2124

You can display <div class="item">and </div> each two iterations, for example checking if $i is an even number:

<?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; ?>

Upvotes: 1

Related Questions