Jump to content

Recommended Posts

Hi all, does anyone know how to perform this, or if it's even possible? I need every 6th row to be a GROUP BY of the following 5 rows, they all share a common group_id so that's what the GROUP BY is performed on.

 

1. GROUP BY group_id

2. item #1

3. item #2

4. item #3

5. item #4

6. item #5

7. GROUP BY group_id

8. item #6

... and so on

Link to comment
https://forums.phpfreaks.com/topic/256322-group-by-every-6th-row/
Share on other sites

What part is it you don't understand? The first row is a GROUP BY, like a summary of the five following rows, which share the same group_id. First comes a summary row, then 5 rows, and it repeats until all rows matching are retrieved.

To perform a special action every time the 6th item is reached (and the first item), you could do something like this:

 

<?php
//CREATE AN ARRAY OF VALUES TO EXPERIMENT WITH
$value_array = array();
for($i=1; $i<=15; $i++) {
$value_array[] = rand(1,500);
}

//LOOP THROUGH THE TEST VALUES
for($i=0; $i < count($value_array); $i++) {
if($i%6 == 0) { $groupBy = true;  }  //for every 6th item, we need to group by (includes the first item)
else          { $groupBy = false; }

print ($groupBy) ? "<div>$i: Group By - $value_array[$i]</div>" : "<div>$i: $value_array[$i]</div>";
}
?>

I suspect you actually want to output a heading every time the group id changes, followed by the data under that heading. If so, see the logic in this post - http://www.phpfreaks.com/forums/index.php?topic=349740.msg1650897#msg1650897

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.