Paulkirkewalker Posted October 26, 2006 Share Posted October 26, 2006 Hi there, I'm relatively new to PHP and mySQL. I am far more used to using Access and VBA so sorry if I use the wrong terminology here.In access it is very straightforward, using reports to group the results of a query. For example, the results:Paul, EdinburghJo, GlasgowNicky, EdinburghJames, Leedscan be easily represented like this:Glasgow JoEdinburgh Nicky PaulLeeds JamesIn Access reports this is called grouping. I need to do the same in PHP/mySQL to show all the images linked to a parent record together under the parent record's details rather than show each as a seperate result with the parent record's details shown for each image in turn.Any ideas?Thanks, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/25157-grouping-query-results/ Share on other sites More sharing options...
sasa Posted October 27, 2006 Share Posted October 27, 2006 put your data in array and try[code]<?php$data = array( array('Paul', 'Edinburgh', '1st'), array('Jo','Glasgow', '2nd'), array('Nicky', 'Edinburgh', '2nd'), array('James', 'Leeds', '2nd'));function grup($data, $key){ foreach ($data as $a) { $b = $a[$key]; unset($a[$key]); $out[$b][] = $a; } return $out;}function pr($a) { foreach ($a as $k => $b) { echo $k,"<br />\n"; foreach ($b as $c) { echo "- "; echo implode(' => ',$c); } echo "\n<br />\n"; } echo "<br />\n";}pr(grup($data,1));echo "<hr />\n";pr(grup($data,2));?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25157-grouping-query-results/#findComment-115253 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.