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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:35:30+00:00 2026-06-09T17:35:30+00:00

This question is about Kohana framework. I’m new to it. Using foreach() I want

  • 0

This question is about Kohana framework. I’m new to it.

Using foreach() I want to display some data. Everything is fine, but I want to group that data 4 items in a row so it looks like:

1st 2nd 3rd 4th
5th 6th 7th 8th
9th 10th 11th 12th
....

That’s why I have to add <div> every 4th time.

How do you make it? Do you use a simple counter and check if its mod is zero? Is there a special Kohana function to check the number of the current $item within foreach() if it is the first, the second or the n-th… item ?

<?foreach ($items as $item): ?>
//add <div> tag for 1st, 4th, 7th, etc item

//do something

//add closing </div> tag for 1st, 4th, 7th, etc item
<? endforeach; ?>
  • 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-09T17:35:32+00:00Added an answer on June 9, 2026 at 5:35 pm

    Using the foreach construct specifying the array key via $key => $item, you can test if $key % 4 == 0 (or maybe $key % 4 == 3 in your case) to close the open <div>.

    // Initial opening div..
    <div>
    <?foreach ($items as $key => $item): ?>
     <?=$item ?>
     <? if ($key % 4 == 3): ?>
    ... Close the open div and open a new one
    </div>
    <div>
    <? endif; ?>
    <? endforeach; ?>
    </div>
    

    The templating syntax hurts my eyes. Here’s proper PHP:

    echo '<div>';
    foreach ($items as $key => $item) {
      echo $item;
    
      if ($key % 4 == 3) {
        echo '</div><div>';
      }
    }
    echo '</div>';
    

    Given the following input:

    $items = array('a','b','c','d','e','f','g','h','i','j','k');
    // Output:
    <div>abcd</div><div>efgh</div><div>ijk</div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a question I had about working with the Kohana framework, though I
I've this question about pass some instances by ref or not: here is my
I have seen this question about deploying to WebSphere using the WAS ant tasks.
Quick question about general MVC design principle in PHP, using CodeIgniter or Kohana (I'm
In this question about creating a new perl module , it was suggested to
My previous question about this subject was answered and I got some tests working
I previously asked this question about how to make versioned serializaion using boost::serialization::access I
I have asked this question about using the a Linq method that returns one
I asked this question about multithreading in servlet, and many people suggest using a
this question about matlab: i'm running a loop and each iteration a new set

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.