MrMastermind Posted October 24, 2006 Share Posted October 24, 2006 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?? Quote Link to comment Share on other sites More sharing options...
Caesar Posted October 24, 2006 Share Posted October 24, 2006 Is the data being called from a db? If not, then may I recommend [url=http://us2.php.net/usort]usort()[/url] Quote Link to comment Share on other sites More sharing options...
MrMastermind Posted October 24, 2006 Author Share Posted October 24, 2006 Thanks!Not it is not called from a db, it's an array of data. I'll have a look at usort().If you have an example, that would be great! Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 24, 2006 Share Posted October 24, 2006 [quote author=MrMastermind link=topic=112579.msg456994#msg456994 date=1161721854]If you have an example, that would be great![/quote]Did you click that link he provided. There are examples there. Quote Link to comment Share on other sites More sharing options...
MrMastermind Posted October 24, 2006 Author Share Posted October 24, 2006 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... Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 25, 2006 Share Posted October 25, 2006 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] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.