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 8806885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:14:15+00:00 2026-06-14T02:14:15+00:00

The following loop groups the items into two groups: First group of 5 items

  • 0

The following loop groups the items into two groups: First group of 5 items in a li and then each group of 6 items. I’m trying to modify this to group first 3 items in a li and then next each 5 items in the li.

for ($i = 1; $i < 20; $i++) {
    echo ($i === 1 || $i % 6 === 0) ? "<li>" : null,
        "<div>item {$i}</div>",
        ($i % 6 === 5) ? "</li>" : null;
}
if ($i % 6 !== 0) echo "</li>";

I’m trying to modify to following, but the grouping does not work fine (eg. wrong number of items goes in a li, some items do not get in li etc.)

for ($i = 1; $i < 20; $i++) {
    echo ($i === 1 || $i % 5 === 0) ? "<li>" : null,
        "<div>item {$i}</div>",
        ($i % 5 === 3) ? "</li>" : null;
}
if ($i % 5 !== 0) echo "</li>";
  • 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-14T02:14:17+00:00Added an answer on June 14, 2026 at 2:14 am

    Try this (Edited):

    echo "<li>";
    for($i = 1; $i < 20; $i++) {
        $mod = $i % 5;
        echo $mod == 4 ? "<li>" : null,
            "<div>item {$i}</div>",
            $mod == 3 ? "</li>" : null;
    }
    if($i % 5 != 3) echo "</li>";
    

    Code example with slight changes

    Explanation:

    If you want to group items in groups of 5, then mod by 5. The first three items are a special case, but since 3 is less than 5, we can incorporate the first three numbers into our main loop.

    The problem with your code is in adding the <li>s. Since we start $i at 1 instead of 0, we should use $i % 5 == 4 instead of $i % 5 == 0.

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

Sidebar

Related Questions

I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I am using the following loop to add items to an an array. I
I have put together the following code, the problem is that each while loop
Firstly I am aware of this question: XSLT: Loop selecting two elements at a
Goal: Group strings into into different letter groups. Example letter groups: A to C,
I have the following loop that renders an array a of arrays. Each arrays
I've got following method in User model def get_employees @employees = [] groups.each do
I wrote the following loop since I have to repeat the same/similar code 25
Say I have the following loop: i = 0 l = [0, 1, 2,
I have the following loop in my viewDidLoad: for(int i=1; i<[eventsArray count]; i++) {

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.