needing help with distinct function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Apophis
Forum Newbie
Posts: 13
Joined: Fri Jul 28, 2006 7:56 am

needing help with distinct function

Post by Apophis »

I am building a database of pedigree of dogs but there is one thing i can't solve.

I want to build a sibling page and that is no problem untill i want to display distinct females and there offsprings with the same sire.

If I do a query to find out witch are the distinct mothers i get unique numbers and he will display them, but i want him to work with the numbers found with distinct function and give me names and offspring.
the output has to be in a formatted html table.

example: row1 female1
row2 offsprings of female1
untill the result of the distinct = 0

The result for the moment is found at http://www.wanyika.be/test/sibling.php?Id=7036
The code i have using untill now for this page is that below.
Now i want him to display below the females and there offsprings. Is there someone ho wants to help me solve this becomming nightmare?

Code: Select all

<?php
include('config.php');

$Id = (isset($_GET['Id'])) ? intval($_GET['Id']) : 0 ; 
$where = ($Id != 0) ? " WHERE Id=".$Id : " ";

	$SQL_single = "SELECT * FROM dog_table WHERE Id = '".$Id."'" or die(mysql_error());
	$SQL_single_Result = mysql_query($SQL_single);
	$SQL_single_Row = mysql_fetch_array($SQL_single_Result);
		
	$SQL_parents = "SELECT FatherId, MotherId FROM dog_table WHERE Id = '".$Id."'" or die(mysql_error());
	$SQL_parents_Result = mysql_query($SQL_parents);
	
	if ($SQL_parents_Result) { 
   		$row = mysql_fetch_assoc($SQL_parents_Result); 
   		$MotherId = $row['MotherId'];
   		$FatherId = $row['FatherId']; 
} 
		else { 
  			echo $SQL_parents; 
}

	$SQL_father = "SELECT * FROM dog_table WHERE Id = $FatherId" or die(mysql_error());
	$SQL_father_Result = mysql_query($SQL_father);
	$SQL_father_Row = mysql_fetch_array($SQL_father_Result);
	
	$SQL_mother = "SELECT * FROM dog_table WHERE Id = $MotherId" or die(mysql_error());
	$SQL_mother_Result = mysql_query($SQL_mother);
	$SQL_mother_Row = mysql_fetch_array($SQL_mother_Result);
	
	$SQL_full_Sibling = "SELECT * FROM dog_table WHERE FatherId = $FatherId AND MotherId = $MotherId and Id !=$Id";
	$SQL_full_Sibling_Result = mysql_query($SQL_full_Sibling) or die(mysql_error());
	$SQL_full_Sibling_Row = mysql_fetch_array($SQL_full_Sibling_Result);
	
		
function Print_Single ($SQL_single_Row) {
	  
	  $Id = $SQL_single_Row['Id'];
	  $DOB1 = $SQL_single_Row['DOB'];
	  $DOB = date('d M Y', strtotime($DOB1));
	  
	  $Image_folder = "<img src=\"../pictures/andere/new.gif\" width=\"35\" height=\"15\" />";
      $new = $Image_folder;
	  $bewerk_datum = $SQL_single_Row['DOC'];  
	  $aantal_dagen = floor(abs((strtotime($bewerk_datum)-time())/(60*60*24)));  

	  $Image_folder2 = "<img src=\"../pictures/andere/update.gif\" width=\"47\" height=\"10\" />";
      $update = $Image_folder2;
	  $bewerk_datum2 = $SQL_single_Row['DOU'];  
	  $aantal_dagen2 = floor(abs((strtotime($bewerk_datum2)-time())/(60*60*24)));
	  
	  print "<span class=\"style2\">".$SQL_single_Row['Ch']."</span>\n";
	  print "</a>&nbsp";
	  print "<span class=\"style3\">".$SQL_single_Row['DogName']."</span";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">(".$SQL_single_Row['Color'].")</span>";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">".$DOB."</span>";
	  print "</a>&nbsp;&nbsp;&nbsp&nbsp";
	  if($aantal_dagen <60){echo $new;}  
      else{echo'';}
      if($aantal_dagen2 <60){echo $update;}  
      else{echo'';} 
	  print "<br><font color='black' size='1' face='Arial'>";
	  print "[<a href=\"pedigree.php?Id=$Id\">Pedigree]</a>&nbsp;&nbsp;";
	  print "[<a href=\"breeding.php?Id=$Id\">Breeding]</a>&nbsp;&nbsp;";
	  print "[<a href=\"sibling.php?Id=$Id\">Sibling]</a></font>";
}		

function Print_Father ($SQL_father_Row){
	  
	  $Id = $SQL_father_Row['Id'];
	  $DOB1 = $SQL_father_Row['DOB'];
	  $DOB = date('d M Y', strtotime($DOB1));
	  
	  $Image_folder = "<img src=\"../pictures/andere/new.gif\" width=\"35\" height=\"15\" />";
      $new = $Image_folder;
	  $bewerk_datum = $SQL_father_Row['DOC'];  
	  $aantal_dagen = floor(abs((strtotime($bewerk_datum)-time())/(60*60*24)));  

	  $Image_folder2 = "<img src=\"../pictures/andere/update.gif\" width=\"47\" height=\"10\" />";
      $update = $Image_folder2;
	  $bewerk_datum2 = $SQL_father_Row['DOU'];  
	  $aantal_dagen2 = floor(abs((strtotime($bewerk_datum2)-time())/(60*60*24)));
	  
	  
	  print "<span class=\"style2\">".$SQL_father_Row['Ch']."</span>\n";
	  print "</a>&nbsp";
	  print "<span class=\"style3\">".$SQL_father_Row['DogName']."</span";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">(".$SQL_father_Row['Color'].")</span>";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">".$DOB."</span>";
	  print "</a>&nbsp;&nbsp;&nbsp&nbsp";
	  if($aantal_dagen <60){echo $new;}  
      else{echo'';}
      if($aantal_dagen2 <60){echo $update;}  
      else{echo'';}
	  print "<br><font color='black' size='1' face='Arial'>";
	  print "[<a href=\"pedigree.php?Id=$Id\">Pedigree]</a>&nbsp;&nbsp;";
	  print "[<a href=\"breeding.php?Id=$Id\">Breeding]</a>&nbsp;&nbsp;";
	  print "[<a href=\"sibling.php?Id=$Id\">Sibling]</a></font>";
}

function Print_Mother ($SQL_mother_Row){
	  
	  $Id = $SQL_mother_Row['Id'];
	  $DOB1 = $SQL_mother_Row['DOB'];
	  $DOB = date('d M Y', strtotime($DOB1));
	  
	  $Image_folder = "<img src=\"../pictures/andere/new.gif\" width=\"35\" height=\"15\" />";
      $new = $Image_folder;
	  $bewerk_datum = $SQL_mother_Row['DOC'];  
	  $aantal_dagen = floor(abs((strtotime($bewerk_datum)-time())/(60*60*24)));  

	  $Image_folder2 = "<img src=\"../pictures/andere/update.gif\" width=\"47\" height=\"10\" />";
      $update = $Image_folder2;
	  $bewerk_datum2 = $SQL_mother_Row['DOU'];  
	  $aantal_dagen2 = floor(abs((strtotime($bewerk_datum2)-time())/(60*60*24)));
	  
	  
	  print "<span class=\"style2\">".$SQL_mother_Row['Ch']."</span>\n";
	  print "</a>&nbsp";
	  print "<span class=\"style3\">".$SQL_mother_Row['DogName']."</span";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">(".$SQL_mother_Row['Color'].")</span>";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">".$DOB."</span>";
	  print "</a>&nbsp;&nbsp;&nbsp&nbsp";
	  if($aantal_dagen <60){echo $new;}  
      else{echo'';}
      if($aantal_dagen2 <60){echo $update;}  
      else{echo'';}
	  print "<br><font color='black' size='1' face='Arial'>";
	  print "[<a href=\"pedigree.php?Id=$Id\">Pedigree]</a>&nbsp;&nbsp;";
	  print "[<a href=\"breeding.php?Id=$Id\">Breeding]</a>&nbsp;&nbsp;";
	  print "[<a href=\"sibling.php?Id=$Id\">Sibling]</a></font>";
}

function Print_full_Sibling ($SQL_full_Sibling_Row){
	  
	  $Id = $SQL_full_Sibling_Row['Id'];
	  $DOB1 = $SQL_full_Sibling_Row['DOB'];
	  $DOB = date('d M Y', strtotime($DOB1));
	  
	  $Image_folder = "<img src=\"../pictures/andere/new.gif\" width=\"35\" height=\"15\" />";
      $new = $Image_folder;
	  $bewerk_datum = $SQL_full_Sibling_Row['DOC'];  
	  $aantal_dagen = floor(abs((strtotime($bewerk_datum)-time())/(60*60*24)));  

	  $Image_folder2 = "<img src=\"../pictures/andere/update.gif\" width=\"47\" height=\"10\" />";
      $update = $Image_folder2;
	  $bewerk_datum2 = $SQL_full_Sibling_Row['DOU'];  
	  $aantal_dagen2 = floor(abs((strtotime($bewerk_datum2)-time())/(60*60*24)));
	  
	  print "<span class=\"style2\">".$SQL_full_Sibling_Row['Ch']."</span>\n";
	  print "</a>&nbsp";
	  print "<span class=\"style3\">".$SQL_full_Sibling_Row['DogName']."</span>";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">(".$SQL_full_Sibling_Row['Color'].")</span>";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">".$DOB."</span>";
	  print "</a>&nbsp;&nbsp;&nbsp&nbsp";
	  if($aantal_dagen <60){echo $new;}  
      else{echo'';}
      if($aantal_dagen2 <60){echo $update;}  
      else{echo'';}
	  print "<br><font color='black' size='1' face='Arial'>";
	  print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp[<a href=\"pedigree.php?Id=$Id\">Pedigree]</a>&nbsp;&nbsp;";
	  print "[<a href=\"breeding.php?Id=$Id\">Breeding]</a>&nbsp;&nbsp;";
	  print "[<a href=\"sibling.php?Id=$Id\">Sibling]</a></font>";
	  
}

function Print_distinct_mother ($SQL_distinct_mother_Row){
	  
	  $sql = "SELECT * FROM dog_table WHERE Id = $Mother";
	  $Mother_Result = mysql_query($sql);
	  $Mother_Row = mysql_fetch_array($Mother_Result);
	  
	  $Id = $Mother_Row['Id]'];
	  $DOB1 = $Mother_Row['DOB'];
	  $DOB = date('d M Y', strtotime($DOB1));
	  
	  $Image_folder = "<img src=\"../pictures/andere/new.gif\" width=\"35\" height=\"15\" />";
      $new = $Image_folder;
	  $bewerk_datum = $Mother_Row['DOC'];  
	  $aantal_dagen = floor(abs((strtotime($bewerk_datum)-time())/(60*60*24)));  

	  $Image_folder2 = "<img src=\"../pictures/andere/update.gif\" width=\"47\" height=\"10\" />";
      $update = $Image_folder2;
	  $bewerk_datum2 = $Mother_Row['DOU'];  
	  $aantal_dagen2 = floor(abs((strtotime($bewerk_datum2)-time())/(60*60*24)));
	  
	  print "<span class=\"style2\">".$Mother_Row['Ch']."</span>\n";
	  print "</a>&nbsp";
	  print "<span class=\"style3\">".$Mother_Row['DogName']."</span>";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">(".$Mother_Row['Color'].")</span>";
	  print "</a>&nbsp";
	  print "<span class=\"style4\">".$DOB."</span>";
	  print "</a>&nbsp;&nbsp;&nbsp&nbsp";
	  if($aantal_dagen <60){echo $new;}  
      else{echo'';}
      if($aantal_dagen2 <60){echo $update;}  
      else{echo'';}
	  print "<br><font color='black' size='1' face='Arial'>";
	  print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp[<a href=\"pedigree.php?Id=$Id\">Pedigree]</a>&nbsp;&nbsp;";
	  print "[<a href=\"breeding.php?Id=$Id\">Breeding]</a>&nbsp;&nbsp;";
	  print "[<a href=\"sibling.php?Id=$Id\">Sibling]</a></font>";
	  
}

print "<table width=\"80%\" height=\"40\" border=\"1\" bgcolor=\"#FFFF00\">";
print   "<tr>";
print     "<td valign=\"top\">";
echo Print_Single($SQL_single_Row);
print  "</td></tr>";
print  "</table>";
print "<table width=\"80%\" border=\"1\" bgcolor=\"#00FFFF\">";
print   "<tr>";
print     "<td width=\"20%\"><div align=\"center\"><strong>Sire</strong></div></td>";
print     "<td width=\"87%\"><div align=\"left\">";
echo Print_Father($SQL_father_Row);
print  "</div></td></tr>";
print "</table>";	
print "<table width=\"80%\" border=\"1\" bgcolor=\"#FFCCCC\">";
print   "<tr>";
print     "<td width=\"20%\"><div align=\"center\"><strong>Dame</strong></div></td>";
print     "<td width=\"87%\"><div align=\"left\">";
echo Print_Mother($SQL_mother_Row);
print  "</div></td></tr>";
print "</table>";
print "<table width=\"80%\" border=\"1\" bgcolor=\"#CCCCCC\">";
print   "<tr>";
print     "<td width=\"20%\"><div align=\"Top\"><strong>Full Siblings - same father/mother</strong></div></td>";
print     "<td width=\"87%\"><div align=\"left\">";

	if (mysql_num_rows($SQL_full_Sibling_Result) >0){
		
			$a = 1;
		
	while($SQL_full_Sibling_Row = mysql_fetch_array( $SQL_full_Sibling_Result)) {

		print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp$a";
		print "&nbsp ";
		echo Print_full_Sibling($SQL_full_Sibling_Row);
		print "<br>";
		$a++;
	}
print  "</div></td></tr>";
print "</table>";
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So you want to see all children from a given father and mother?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look into the hierarchical database structures we've kindly linked to from the Database board's stickies.
Apophis
Forum Newbie
Posts: 13
Joined: Fri Jul 28, 2006 7:56 am

What i want to display

Post by Apophis »

First i want him to display all matings with the same father but different mother. Thats why i use distinct mother and get the numbers of the mothers so it wil be displayed in

example: row1 female1
row2 offsprings of female1
untill the result of the distinct = 0

Ofcourse if i have solved this i can make one with the opposite combination (same mother different father)
Apophis
Forum Newbie
Posts: 13
Joined: Fri Jul 28, 2006 7:56 am

Post by Apophis »

Feyd i really don't know what you are saying here. Can you give me a link to the page you are referring to
feyd wrote:look into the hierarchical database structures we've kindly linked to from the Database board's stickies.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Oops.. I just realized the link is in Client Side. Why.. I have no idea.

this leads to this.
Post Reply