James Brandon
James Brandon

Reputation: 1400

PHP While Loop wrapping every 4 results with a li

i am trying to wrap each 4 results inside a LI and repeat for every 4 items like

<li>
 <div>item 1</div>
 <div>item 2</div>
 <div>item 3</div>
 <div>item 4</div>
</li>

PHP so far.... the loop ive attempted is of course not working :)

if ( $query->have_posts() ) {
    $opnews .= '<ul class="newsitems orbit-slides-container">';
    $count = 0;
    while ( $query->have_posts() ) : $query->the_post();
        $post_id = get_the_ID();
        $opnews_item_data = get_post_meta( $post_id, 'opnews_item', true );
        if ($i%4 == 1) {
            $opnews .= '<li>';
        }
        $opnews .= '<div class="columns large-3 small-12 medium-3">';
        $opnews .= '<div class="panel green opacity-change">';
        $opnews .= '<h1>' . get_the_title() . '</h1>';
        $opnews .= get_the_content_with_formatting();
        $opnews .= '</div>';
        $opnews .= '</div>';
        if ($count%4 == 0) {
            $opnews .= '</li>';
        }
    endwhile;
    $opnews .= '</ul>';
    wp_reset_postdata();
}

Upvotes: 1

Views: 1646

Answers (4)

Jeff Lambert
Jeff Lambert

Reputation: 24661

It looks like you're mixing both $i and $count. One of them you're using the modulous operator and comparing if the remainder after division is 1, and the other you're comparing if the remainder is 0. Neither of them seems to be incrementing (and $i doesn't look to be defined from the snippet you've provided).

Choose one, $count, and compare it with 0 using the modulous and be sure to increment it within the loop:

if ($count % 4 == 0) {
    $opnews .= '<li>';
}

// ...
$count++;

if ($count % 4 == 0) {
    $opnews .= '</li>';
}

Upvotes: 0

Cl&#233;ment Malet
Cl&#233;ment Malet

Reputation: 5090

You are using $i and $count, so pick only one.

Then you have to increment it between your <li> to get it working.

And finally, you should check, once you finished the loop, that the last <li> has been echoed or you will get some trouble (a missing </li>)

$array = range(1, 9);

$i = 0;
foreach ($array as $val) {
  if ($i%4 == 0) echo '<li>';
  $i++;
  echo $val;
  if ($i%4 == 0) echo '</li>';
}
if ($i%4 != 0) echo '</li>';

Output :

<li>
  1 2 3 4
</li>
<li>
  5 6 7 8
</li>
<li>
  9
</li>

Upvotes: 2

Reed Oei
Reed Oei

Reputation: 1508

It's not working because you never change count. Count is always 0, so $count % 4 == 0 is always true. Also, unless it's somewhere else you haven't defined i.

Use only count (or only i).

if ($count % 4 == 0) {
    $opnews .= '<li>';
}

DO STUFF HERE

$count += 1

if ($count % 4 == 0) {
    $opnews .= '</li>';
}

Upvotes: 0

Mike
Mike

Reputation: 8877

The modulus operator (%) divides the number and returns the remainder. So, your line if ($i%4 == 1) probably isn't what you're after, as if it's every 4th row, you'll want it with no remainder.

The $count%4 == 0 line also doesn't make much sense to me, as you're not incrementing the number. You're also not incrementing $i.

Try the following:

if ( $query->have_posts() ) {
    $opnews .= '<ul class="newsitems orbit-slides-container">';
    $i = 0;
    while ( $query->have_posts() ) : $query->the_post();
        $post_id = get_the_ID();
        $opnews_item_data = get_post_meta( $post_id, 'opnews_item', true );
        if ($i%4 == 0) {
            if ($i != 0){
                $opnews .= '</li>';
            }
            $opnews .= '<li>';
        }
        $opnews .= '<div class="columns large-3 small-12 medium-3">';
        $opnews .= '<div class="panel green opacity-change">';
        $opnews .= '<h1>' . get_the_title() . '</h1>';
        $opnews .= get_the_content_with_formatting();
        $opnews .= '</div>';
        $opnews .= '</div>';
        $i++;
    endwhile;
    $opnews .= '</li>';
    $opnews .= '</ul>';
    wp_reset_postdata();
}

Upvotes: 1

Related Questions