Q:

PHP SQL Loop results based on field

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?

A:

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

