Jump to content

Family Tree Problem


ThunderAI

Recommended Posts

I am trying to make a family tree application and drawing out the tree has be a little confused as to what migt be the best way to connect to the database.

 

I have two tables, a list of people and another describing the relationships between the people.

 

One way to do this would be to start at a certain point in the tree, create a new connection to display their childern, then create another one to display their childern, and then their childern and so forth.  When all layers are done with the first found child, the cycle would start over with whatever other siblings that child had.  But this process requies me to manualy create a new connection a set number of times and i dont want 15 layers of connections.

 

Is there some way to automate this process so that one connection can start at the start point, and just continue to cycle through records until it finds so more records to display?

Link to comment
Share on other sites

I am trying to make a family tree application and drawing out the tree has be a little confused as to what migt be the best way to connect to the database.

Depends on your database.

Oracle supports hierarchical views, which allow you to traverse parent<-->child relationships. No issues at all with multiple children from one parent, but I've been trying to figure out a way of handling multiple parent nodes for ages without success.

Link to comment
Share on other sites

I just dont know how to automate it at all.

 

I can use while to loop through the results array but each new cycle would require an additonal connection to be made into the database.  What I am thinking is this:

 

<?php
$sql = "";
$objconn_support = mysqli_connect("localhost", "webuser", "limitaces", "dahlweb");
if (mysqli_connect_errno()) {
	printf("connect failed: %s\n", mysqli_connect_error());
	exit();
}
else {
	$objrs_support = mysqli_query($objconn_support, $sql);
	if ($objrs_support) {
			while ($objfields = mysqli_fetch_array($objrs_support, MYSQLI_ASSOC)) {
					// Cycle First Level 
					$sql2 = "where ID is equal to this element of First Level";
					$objconn_support2 = mysqli_connect("localhost", "webuser", "limitaces", "dahlweb");
					if (mysqli_connect_errno()) {
							printf("connect failed: %s\n", mysqli_connect_error());
							exit();
						}
						else {
							$objrs_support2 = mysqli_query($objconn_support, $sql2);
							if ($objrs_support2) {
									while ($objfields2 = mysqli_fetch_array($objrs_support2, MYSQLI_ASSOC)) {
											// Cycle Second Level 						
											$sql2 = "where ID is equal to this element of First Level";
											$objconn_support3 = mysqli_connect("localhost", "webuser", "limitaces", "dahlweb");
											if (mysqli_connect_errno()) {
													printf("connect failed: %s\n", mysqli_connect_error());
													exit();
												}
												else {
													$objrs_support3 = mysqli_query($objconn_support, $sql3);
													if ($objrs_support3) {
															while ($objfields3 = mysqli_fetch_array($objrs_support3, MYSQLI_ASSOC)) {
																	// Cycle Second Level 
?>

 

You can see how that would be too clunky to do to any serious depth like a family tree. It works for a menu system, but its not realistic for the purpose i need it for.

Link to comment
Share on other sites

The solution to your problem is called recursion. A recursive function is a function that calls itself over and over again doing something slightly different every time until a stop condition is encountered. For example, in pseudo-code your application would look something like this:

 

$link = mysqli_connect(...);

// A node is considered a person in the tree, a node has parents and children.
function displayTreeNode($node)
{
    1. Use the existing database connection ($link) to obtain the data about the node.
    2. Display the current node.
    3. Check for any relationships to other nodes and call displayTreeNode() for each of those nodes.
    4. If no more relationships exist, exit the function.
}

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.