Jump to content

Recommended Posts

In my database are following fields: id, fatherid, motherid.

There are four "starter-dogs" with id 1,2,3 and 4; now i want to count how many generations are between the starter and the dog (for each starter seperate).

 

How can i loop the query (and count) until it reach id 1 (or 2)?

 

Select fatherid from mytable where id = 222

Result (for example) 333 - not 1 - again

Select fatherid from mytable where id = 333 ...

Link to comment
https://forums.phpfreaks.com/topic/279910-count-generation-maybe-loop-until/
Share on other sites

ok, but here are same dogs more than one time in pedigree (the starter dogs).

On father side the starterdog is in the 6. generation, on mother side in 9. generation.

With this solution a called every fatherid so long until starter dog is reached (for father, grandfather, grandgrand...)

 

I want the shortest distance to the starter dog...

http://www.wolfdog-database.com/dog/dog.php?id=13425&gens=7

 

for example here the shortest distance to Sarik. In other cases the pedigree is over much more generations ;-)

Breadth-first search: every time you find a non-starter dog, append it to an array (as well as the "depth"). Use a while/array_shift() loop to keep pulling stuff off the array (you can't foreach) until you find a starter dog.

$dogs = array(current dog);
while($dog = array_shift($dogs)) {
	list($dog, $depth) = $dog;
	if $dog is a starter dog {
		return $depth;
	} else {
		$dogs[] = array($dog''s mother, $depth + 1);
		$dogs[] = array($dog''s father, $depth + 1);
	}
}
Edited by requinix
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.