Jump to content

Display array table grouped by array value


MrMastermind

Recommended Posts

I'm trying to display results from an array in an HTML table, but I want to group the results by a value in the array. Something like:

array[
[id, title, groupname, date]
[id, title, groupname, date]
]

And then, group by the groupname. So that I can wrap each group in a DIV to display and hide the rest of the info. How would I loop through the array to group the info??
Yes, I did click the link. But maybe I don't understand it correctly. But usort() will sort my array. This helps a bit, but then I still have to loop through the array (using a foreach()) and compare values to either start a new div and table or to add a row to the current table.
So, I can sort of explain what I want to achieve, but can't get my head around it to write the code...
Assuming $results is the array, this should be close to what you are looking for.

[code]
$currentGroup = "";

foreach ($results as $row) {
  if ($row[groupname] != $currentGroup) {
    if ($currentGroup != "") { echo "</div>"; }
    echo "<div>";
    $currentGroup = $row[groupname];
  }
  echo $row[groupname] . "<br>";
}
echo "</div>";
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.