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??
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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.