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

   }
}

Link to comment
Share on other sites

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

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.