ThunderAI Posted November 27, 2008 Share Posted November 27, 2008 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 https://forums.phpfreaks.com/topic/134525-family-tree-problem/ Share on other sites More sharing options...
Mark Baker Posted November 27, 2008 Share Posted November 27, 2008 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 https://forums.phpfreaks.com/topic/134525-family-tree-problem/#findComment-700458 Share on other sites More sharing options...
ThunderAI Posted November 27, 2008 Author Share Posted November 27, 2008 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 https://forums.phpfreaks.com/topic/134525-family-tree-problem/#findComment-700486 Share on other sites More sharing options...
xdracox Posted November 27, 2008 Share Posted November 27, 2008 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 https://forums.phpfreaks.com/topic/134525-family-tree-problem/#findComment-700494 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.