Iterating over every second element in forloop untill there is no row left

  for-loop, loops, php

I’m trying to make a website that displays all userprofiles. I want to wrap every two users in a <div>, until there is no left. So say we have 19 arrays consisting of userprofileinfo in $info – the following code will work fine, but will print out the 20th out as an empty div, which I want to prevent. What to do here?

$info = mysqli_fetch_all($result, MYSQLI_ASSOC);

for ($count = 0; $count <= mysqli_num_rows($result) - 1; $count += 2) {
    echo "<div>".print_r($info[$count])."</div";
    echo "<div>".print_r($info[$count+1])."</div";
}   

Source: Ask PHP

LEAVE A COMMENT