将具有父子关系的项目保存到数据库的算法

时间:2014-12-01 16:23:28

标签: recursion parent-child menu-items

我需要帮助设计我正在处理的应用程序的逻辑。 应用程序应该允许用户创建思维导图并将其保存到mysql数据库以供以后编辑。 每个思维导图都由相互关联的节点组成,这意味着一个节点应该有一个父节点和它所属的思维导图的id。 我被困在这里。如何将节点保存到数据库,并能够从查询结果中查询和重建思维导图树。

Root
   Child1
   Child2
      GrandChild1
    GreatGrandChild1
    GreatGrandChild1
   Child3
      GrandChild2

我需要一个算法,它可以保存节点,并且能够找出与我给出的树类似的项目的关系/顺序。这非常类似于在Wordpress中保存和检索菜单的方式,但我无法找到正确的逻辑来执行此操作。 我知道这里真的很棒。请帮忙。

2 个答案:

答案 0 :(得分:1)

这在3列表中非常简单。

Column-1: id, Column-2: name, Column-3: parent_id
例如,数据将是这样的:

 1    ROOT   NULL
 2    Child1  1
 3    Child2  1
 ... and so on..

答案 1 :(得分:0)

我终于找到了解决方案。这是我的完整代码。

require_once('config.php');//db conn
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME);
$nav_query = MYSQL_QUERY("SELECT * FROM `nodes` ORDER BY `id`");
$tree = "";                         // Clear the directory tree
$depth = 1;                         // Child level depth.
$top_level_on = 1;               // What top-level category are we on?
$exclude = ARRAY();               // Define the exclusion array
ARRAY_PUSH($exclude, 0);     // Put a starting value in it

WHILE ( $nav_row = MYSQL_FETCH_ARRAY($nav_query) )
{
     $goOn = 1;               // Resets variable to allow us to continue building out the tree.
     FOR($x = 0; $x < COUNT($exclude); $x++ )          // Check to see if the new item has been used
     {
          IF ( $exclude[$x] == $nav_row['id'] )
          {
               $goOn = 0;
               BREAK;                    // Stop looking b/c we already found that it's in the exclusion list and we can't continue to process this node
          }
     }
     IF ( $goOn == 1 )
     {
          $tree .= $nav_row['name'] . "<br>";                    // Process the main tree node
          ARRAY_PUSH($exclude, $nav_row['id']);          // Add to the exclusion list
          IF ( $nav_row['id'] < 6 )
          { $top_level_on = $nav_row['id']; }

          $tree .= build_child($nav_row['id']);          // Start the recursive function of building the child tree
     }
}

FUNCTION build_child($oldID)               // Recursive function to get all of the children...unlimited depth
{
    $tempTree='<ul>';
     GLOBAL $exclude, $depth;               // Refer to the global array defined at the top of this script
     $child_query = MYSQL_QUERY("SELECT * FROM `nodes` WHERE parent_id=" . $oldID);
     WHILE ( $child = MYSQL_FETCH_ARRAY($child_query) )
     {
          IF ( $child['id'] != $child['parent_id'] )
          {
               FOR ( $c=0;$c<$depth;$c++ )               // Indent over so that there is distinction between levels
               { $tempTree .= "&nbsp;"; }
               $tempTree .= "<li>" . $child['name'] . "</li>";
               $depth++;          // Incriment depth b/c we're building this child's child tree  (complicated yet???)
               $tempTree .= build_child($child['id']);          // Add to the temporary local tree
               $depth--;          // Decrement depth b/c we're done building the child's child tree.
               ARRAY_PUSH($exclude, $child['id']);               // Add the item to the exclusion list
          }
     }

     RETURN $tempTree.'</ul>';          // Return the entire child tree
}

ECHO $tree;

?>

这是基于此处找到的代码http://psoug.org/snippet/PHP-Recursive-function-to-generate-a-parentchild-tree_338.html我希望这对某人也有帮助