Jump to content

Output of mysql group with two "things"


Tanja

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/273742-output-of-mysql-group-with-two-things/
Share on other sites

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>';
    }

   }
}

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>';

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.