-1

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

根
   孩子1
   孩子2
      孙子1
    GreatGrandChild1
    GreatGrandChild1
   孩子3
      孙子2

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

4

2 回答 2

1

这在 3 列表中非常容易。

Column-1: id, Column-2: name, Column-3: parent_id

例如,数据将是这样的:

 1    ROOT   NULL
 2    Child1  1
 3    Child2  1
 ... and so on..
于 2014-12-01T16:31:07.807 回答
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我希望这也对某人有所帮助

于 2014-12-03T10:23:26.400 回答