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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:58:17+00:00 2026-05-31T21:58:17+00:00

I’m looking for the right logic to loop thru a recordset and fire an

  • 0

I’m looking for the right logic to loop thru a recordset and fire an event each n times.

Searching on Google i’ve found some discussion on similar situations, but it seems that solutions don’t fits my needs.

I need to interface my app with a webservice that use minOccurs=”0″ maxOccurs=”4″ for repeatable elements in a single call. Let’s say i have a recordset of 22 rows.

I need to:

  1. loop thru the recordset and populate the array $data with each row
  2. on the 4th row (8th, 12th, 16th…) fire the event (call the WS with $data of 4 elements)
  3. empty $data and continue with the loop till the next 4th
  4. if the number of rows is not multiple of 4 i must handle the remainder and fire one last call

Using the modulus operator as shown in this answer, if($i % 4 == 0), i get the event fired each 4 rows, but 22 its not a multiple of 4 so the event is fired till the 20th row and then nothing. Maybe i need to make a division counting rows in ‘excess’?

Since the recordset will be between 50 and 200 rows i think its not necessary run multiple query of 4 rows, am I wrong?

Thanks in advance!

UPDATE:
Inspired by the answers below i finally managed to get the script work as i wanted… probably its not an elegant solution but works as expected:

$result = $query->result(); // the recordset
$total_rows = count($result);
$interleave = 4;
$reminder = $total_rows % $interleave;
$round_rows = ($total_rows-$reminder)+1; // +1 because $i dont start at zero
$data = array();

Start with a initial loop ($round_rows is a multiple of 4)

for ($i=1; $i<$round_rows; $i++){
            $data[$i] = $result[$i];
            if ($i % $interleave == 0){
                $this->fire_event($data);
                $data = array();
            }
 }

Then if there is a reminder, loop thru…

if ($total_rows % $interleave !== 0){
            for ($i = $round_rows; $i < $total_rows + 1; $i++) {
                $data[$i] = $result[$i];
            }
            $this->fire_event($data);
}

Any advice is welcome!

  • 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-05-31T21:58:18+00:00Added an answer on May 31, 2026 at 9:58 pm

    If you need to fire an event at the end of the recordset, just do it.

    for($i=0 .... ) {
     // do yourcalculation
    }
    
    //fire your event for the end of the recordset
    

    If you need to fire the event only when you have spare records after the last event fired then use an if

    $interleave = 4; // or whaterver you interval you need
    for($i=0 .... ) {
     // do yourcalculation
    }
    
    if (!($i % $interleave)) {
        // fire your event
    }
    

    The $i index continue to live after the end of the loop and you can leverage on this language property

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to loop through a bunch of documents I have to put

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.