Tanja Posted January 28, 2013 Share Posted January 28, 2013 first my query and output: function getdog() { $offspringresult = " SELECT dog.id, dog.dogname, dog.father_id, dog.mother_id, dog.date_of_birth, b.dogname AS maker, CONCAT(dog.gender, '.png') AS sex FROM dog INNER JOIN dog b ON dog.father_id = b.id WHERE dog.mother_id='".$_GET['projectdescription1']."' ORDER BY dog.dogname DESC "; $result = mysql_query($offspringresult); $grouped = array(); while ($row = mysql_fetch_object($result)) { $grouped[$row->date_of_birth][] = $row; } return $grouped; } $dog = getdog(); print("<pre>"); print_r($dog); print("</pre>"); foreach ($dog as $date_of_birth => $entries) { echo '<h2>' . $date_of_birth . '</h2>'; foreach ($entries as $entry) { echo '<p>' . $entry->dogname . '<img src=" '.$entry->sex.' "/></p>'; echo '<p>' . $entry->maker. '</p>'; } } Output is like this: 2008-02-08 Connor from Bandit's World Dat Šedý chlup Chunami from Bandit's World Dat Šedý chlup Chica from Bandit's World Dat Šedý chlup Cherina from Bandit's World Dat Šedý chlup Chandra from Bandit's World Dat Šedý chlup Cazan from Bandit's World Dat Šedý chlup 2007-01-12 Bruce from Bandit's World Bandit Blaidd from Bandit's World Bandit Bayana from Bandit's World Bandit Balko from Bandit's World Bandit Bakira from Bandit's World Bandit I want to have output like this: 2008-02-08 Dat Šedý chlup Connor from Bandit's World Chunami from Bandit's World Chica from Bandit's World Cherina from Bandit's World Chandra from Bandit's World Cazan from Bandit's World 2007-01-12 Bandit Bruce from Bandit's World Blaidd from Bandit's World Bayana from Bandit's World Balko from Bandit's World Bakira from Bandit's World what must i change to get this? Dat and Bandit are the "maker" in query. And second: this is an query for a female. But I need "variabel" for male, too - depending on url called dog. How can i do this? Quote Link to comment https://forums.phpfreaks.com/topic/273742-output-of-mysql-group-with-two-things/ Share on other sites More sharing options...
Barand Posted January 28, 2013 Share Posted January 28, 2013 In your function while ($row = mysql_fetch_object($result)) { $grouped[$row->date_of_birth][$row->maker] = $row; } Then foreach ($dog as $date_of_birth => $entries) { foreach ($entries as $maker => $entryArr) { echo "<h2>$date_of_birth - $maker</h2>"; foreach ($entryArr as $entry) { echo '<p>' . $entry->dogname . '<img src=" '.$entry->sex.' "/></p>'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/273742-output-of-mysql-group-with-two-things/#findComment-1408830 Share on other sites More sharing options...
Tanja Posted January 29, 2013 Author Share Posted January 29, 2013 Thanks for answer. When I change funktion to $grouped[$row->date_of_birth][$row->maker] = $row; i get an array like this: Array ( [2008-02-08] => Array ( [Dat Šedý chlup] => stdClass Object ( [id] => 14337 [dogname] => Cazan from Bandit's World [date_of_birth] => 2008-02-08 [maker] => Dat Šedý chlup ) ) [2007-01-12] => Array ( [bandit] => stdClass Object ( [id] => 15865 [dogname] => Bakira from Bandit's World [date_of_birth] => 2007-01-12 [maker] => Bandit ) ) [2006-01-15] => Array ( [bandit] => stdClass Object ( [id] => 15862 [dogname] => Aiyana from Bandit's World [date_of_birth] => 2006-01-15 [maker] => Bandit ) ) ) here is only one puppy from each father, but he have done more and in output i get Notice: Trying to get property of non-object in /www/htdocs/w00fa8ed/search_dog4b.php on line 543 Line 543 ==> echo '<p>' . $entry->dogname . '<img src=" '.$entry->sex.' "/></p>'; Quote Link to comment https://forums.phpfreaks.com/topic/273742-output-of-mysql-group-with-two-things/#findComment-1408906 Share on other sites More sharing options...
Barand Posted January 29, 2013 Share Posted January 29, 2013 sorry, it should be $grouped[$row->date_of_birth][$row->maker][] = $row; Quote Link to comment https://forums.phpfreaks.com/topic/273742-output-of-mysql-group-with-two-things/#findComment-1408912 Share on other sites More sharing options...
Tanja Posted January 29, 2013 Author Share Posted January 29, 2013 sorry... - you are a real cutie and my hero Works perfect! Quote Link to comment https://forums.phpfreaks.com/topic/273742-output-of-mysql-group-with-two-things/#findComment-1408922 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.