【问题标题】:Algorithm to Save Items with Parent-Child Relationship to Database将具有父子关系的项目保存到数据库的算法
【发布时间】:2014-12-03 18:23:26
【问题描述】:

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

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

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

【问题讨论】:

  • 你建议如何识别给定节点?
  • 每个节点都有一个id和它所属的map id。我的数据库结构如下所示。思维导图树表{ id |姓名 |用户 ID | } 然后节点映射看起来像这样 - { id |姓名 | parent_node_id | map_tree_id }.
  • 这个表示如何让你知道一个节点的子节点的顺序?
  • 如果您有节点的表示,为什么要问如何将节点保存到数据库?
  • 嗨,斯科特。对不起,我想我不清楚我想要什么。当在树中给出时,我需要一种算法来将节点保存到数据库中,就像我给出的示例中一样,还需要一种从查询结果重建树的方法。假设您有一个像菜单一样的线程化 html 列表,我如何将列表项保存到数据库中,并能够从选择查询的结果中重建相同的列表。

标签: recursion parent-child menu-items


【解决方案1】:

这在 3 列表中非常容易。

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

例如,数据会是这样的:

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

【讨论】:

  • 我的数据库结构是这样的。思维导图树表{ id |姓名 |用户 ID | } 然后节点映射看起来像这样 - { id |姓名 | parent_node_id | map_tree_id }。我的问题是想出一种算法来从节点表上的选择查询中重建映射树。
【解决方案2】:

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

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 我希望这对某人也有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多