Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:37:34+00:00 2026-06-08T21:37:34+00:00

I am coding a PHP foreach loop. I am using the following code to

  • 0

I am coding a PHP foreach loop. I am using the following code to ensure that only 3 items are grouped into a .slide div container.

<?php 
$count = 0;
foreach ($listing as $item):?>
<div class='slide>

  <div class='item'>Item</div>

<?php if ($count++ % 3 == 1 ): ?>
</div>
<div class="slide">
<?php endif; ?>

<?php endforeach ?>

I need to always have 3 items per group. What is the best way to add items from the start of the array to fill up the remaining items?

EDIT:

An example of the markup I need is:

<div class='slide'>
  <div class='item'>Item 1</div>
  <div class='item'>Item 2</div>
  <div class='item'>Item 3</div>
</div>
<div class='slide'>
  <div class='item'>Item 4</div>
  <div class='item'>Item 1</div>
  <div class='item'>Item 2</div>
</div>

So if there is not 3 items per .slide the array starts again to fill it up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:37:36+00:00Added an answer on June 8, 2026 at 9:37 pm

    One simple solution is to append 1 or 2 items (if required) to the $listing variable before the loop.

    if (count($listing)%3!=0) {
        //Append 1 or 2 items from start of array if needed
        $listing = array_merge($listing, array_slice($listing, 0, 3-count($listing)%3));
    }
    

    The array_slice call returns an array containing items from 0 to 3 - (size of array) mod 3 (either 1 or 2). The array_merge function then adds these 1 or 2 items to the original $listing array. The complete code (with some other minor improvements/fixes) is below:

    <?php 
        $count = 0;
        $listing = array(1, 2, 3, 4, 5, 6, 7);
        if (count($listing)%3!=0) {
            //Append 1 or 2 items from start of array if needed
            $listing = array_merge($listing, array_slice($listing, 0, 3-count($listing)%3));
        }
        ?><div class='slide'> <?php
        foreach ($listing as $item):
            if (($count>0) and ($count%3==0)):
                ?></div><div class="slide"><?php
            endif; 
            ?><div class='item'>Item <?=$item?></div><?php
            $count++;
        endforeach;
        ?></div><?php
    ?>
    

    Depending on your data, you might need to add special checks for the case where the original $listing has 0 or 1 items. Also, probably a good idea not to modify the original array and instead use a copy to append items to and use in the foreach loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that converts my twitter account rss feed into a
I need to know how to do the following using php coding please take
Just to give a little background: I'm using Dreamweaver CS5 for coding php, XAMPP
I have the following code that is presenting a 'word-of-the-day', As I am relatively
I am coding in php and the code takes data from an array to
I am coding a movie archive script using codeigniter for myself with PHP. I
I have decided to start coding using OOP and a PHP framework. I have
I have a strange problem where a for loop in PHP only returns the
I'm coding in PHP/MySQL and have the following query to fetch products and product
I am using the PHP and AJAX coding below to populate a map showing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.