0

I am creating a dynamic recordset where I populate a couple values to the query used to select all inked records.

Next I need loop and group the results but am unsure how to group by the values used in the query.

Reference query:

SELECT fldItem, fldDescription, fldRank,fldType FROM tableName
WHERE fldType IN (33, 34, 36) ORDER BY fldRank

The values 33,34 and 36 are going to be passed dynamically to the query. My goal is to then have a loop/repeat region that allows display in groups with all the results that share the same fldType

Results should look like this:

item1, description1, rank1, type33
item2, description2, rank2, type33
item3, description3, rank5, type33
item4, description4, rank6, type33
item5, description5, rank8, type33

--------

item6, descriptionv, rank1, type34
item7, descriptionw, rank2, type34
item8, descriptionx, rank3, type34
item9, descriptiony, rank4, type34
item10, descriptionz, rank5, type34

--------

item11, descriptionA, rank2, type36
item12, descriptionB, rank3, type36

using a do / while loop does displays all the items to display but not with any grouping. Is a ForEach loop the solution?

3
  • Have you tried different SQL statements with a GROUP BY? That would return the results in groups. You could then use a loop and when the Type changes, have it drop out a line break, or whatever you want to distinguish the different types. Commented Mar 29, 2012 at 15:41
  • 1
    GROUP BY does not return the results in groups, it returns a single result for a group. I.e., using GROUP BY here would result in getting just three rows back. Commented Mar 29, 2012 at 15:52
  • Yes initially I was trying GROUP BY but found it not working when used with the selection IN () Commented Mar 29, 2012 at 15:56

2 Answers 2

3

If you're talking about a visual grouping in your report output, then you'll need to track that value for each loop iteration, and then issue your visual break when you detect a change. Something like this:

$last_type = null;
foreach ($records as $record) {
    if ($last_type !== null && $last_type != $record->fldType) {
        echo '<whatever your visual cue is>';
        $last_type = $record->fldType;
    }
}

Also, make sure you do:

ORDER BY fldType, fldRank
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I believe this is what I am looking for. Thanks!
1
SELECT fldItem, fldDescription, fldRank,fldType FROM tableName
WHERE fldType IN (33, 34, 36) ORDER BY fldType, fldRank

This will group by fldType first, then by fldRank. In your PHP code, just remember the last fldType, and add a new group whenever it changes. All fldTypes will be grouped together now, so you can just loop through the results in order.

1 Comment

Yes, this returns the results in the manner referenced above but I am trying to get groups such as displayed by the "--------" breaks. In actual conditions this is because the results (each group) are to be set to a formatted table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.