twan
twan

Reputation: 2659

Retrieve two sets of data in one looped element

I got an owl carousel that shows two testimonials every slide. But the two slides are inside 1 element I want to loop. Every testimonial comes from the database.

So for example I got my loop like this:

foreach($array as $testimonial){
    $testimonials .= '
    <div class="testwrapper">
        <div class="testimonial">
            '.$testimonial['title'].'
        </div>
        <div class="testimonial">
            '.$testimonial['title'].'
        </div>
    </div>';
}

The two titles are each from different testimonials, I figure I have to use their number in the array, but how can I do that? So array value 0,1 on the first slide then 2,3 on the second etc.

Current code after answer by 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;

Upvotes: 0

Views: 30

Answers (1)

RichardBernards
RichardBernards

Reputation: 3097

Something like this... Using a for-loop:

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>';
}

Upvotes: 1

Related Questions