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

  • Home
  • SEARCH
  • 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 9127069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:11:51+00:00 2026-06-17T07:11:51+00:00

I am trying to group each 5 loop items inside a <li></li> and then

  • 0

I am trying to group each 5 loop items inside a <li></li> and then further group the each li item into two groups, so that first item of each li is in one group, while other 4 are in other group.

With the following code, I can wrap each 5 items of a loop in a li, but I am not able to group each li items into 2 groups. Since there are more than 10 items in the loop, thats why I can not hard code the values of $i to print the div.

$i = 1;
while ($i < 10){
    echo ($i % 5 === 0) ? "<li>" : null;
        $i++;
        echo item $i;
    echo ($i % 5 === 0) ? "</li>" : null;   
}
echo ($i % 5 !== 0) ? "</li>" : null;

This is the desired output:

<li>
    <div class="left">
        Item 1
    </div>
    <div class="right">
        Item 2
        Item 3
        Item 4
        Item 5
    </div>
</li>

<li>
    <div class="left">
        Item 6
    </div>
    <div class="right">
        Item 7
        Item 8
        Item 9
        Item 10
    </div>
</li>

Demo: http://codepad.org/OztLPai8

  • 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-17T07:11:53+00:00Added an answer on June 17, 2026 at 7:11 am

    Working example: http://codepad.org/uHYHl6MD

    <?php
    // Initial group size
    $groupSize = 5;
    $total = 22;
    
    // To keep track of the group
    $groupCounter = $groupSize;
    
    $isFirst = true;
    $i = 1;
    
    while($i < $total){
        echo "<li>\n";
        // Left div
        if($isFirst){
            echo "\t<div class='left'>\n";
            echo "\tItem $i\n";
            $isFirst = false;
            $i++;
            echo "\t</div>\n";
        }
    
        // Right div
        echo "\t<div class='right'>\n";
        while($i <= $groupCounter && $i <= $total){
            echo "\tItem $i\n";
            $i++;
        }
        echo "\t</div>\n";
    
        // Get to the next group
        $groupCounter += $groupSize;
    
        // Start with the first, first.
        $isFirst = true;
    
        echo "</li>\n";
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following loop groups the items into two groups: First group of 5 items
I'm trying to group the following string into three groups. 0:0:Awesome:awesome That being "
I am trying to create a report that has a summary for each group.
I'm trying to group together radiobuttons that are creating using a for loop and
Trying to loop thru a result that has items. Items have a type and
I am trying to write a loop using XSLT so that it automatically groups
I'm trying to group posts from same day, the problem is that 2/20 gets
I'm trying to get the indexes for each pattern that I find in a
I am trying to use a .each loop to find specific elements in a
I'm trying to create a parametric query that will be executed in a loop

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.