Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在mysql中保存树数组_Php_Mysql_Cakephp 2.3 - Fatal编程技术网

Php 在mysql中保存树数组

Php 在mysql中保存树数组,php,mysql,cakephp-2.3,Php,Mysql,Cakephp 2.3,上面的树数组需要以父id保存在mysql中,因此结果如下: Array ( [Root] => Array ( [Parent0] => Array ( [Child0] => Child0 ) [Parent1] => Array (

上面的树数组需要以父id保存在mysql中,因此结果如下:

Array
(
    [Root] => Array
        (
            [Parent0] => Array
                (
                    [Child0] => Child0
                )

            [Parent1] => Array
                (
                    [Child1] => Child1
                )

        )

)
任何人请让我知道我如何可以保存在mysql使用php以上结果


提前感谢您的快速响应。

如果您想实现父子树,您可以使用下面的代码n depth

id  parent_id   name
1   0           Root
2   1           Parent0
3   2           Child0
4   1           Parent1
5   4           Child1
首先创建一个名为“categories”的数据库表,该表包含字段

 Automobile
        Fuel
            Gasoline
            Diesel
        Maintenance
    Food
        Fish
        Pork
-类别识别码(PK int)
-家长id(int)
-标题(varchar)

您可以复制粘贴上述代码,然后完成:)

尝试以下操作:已编辑

- category_id (PK int)
- parent_id (int)
- title (varchar)

<?php
    $connect = mysql_connect("localhost", "root", "") or die ( mysql_error() );
    mysql_select_db("test");
    $nav_query = mysql_query("SELECT * FROM `categories` ORDER BY `category_id`") or die( mysql_error() );
    $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['category_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['title'] . "<br>"; // Process the main tree node
          array_push($exclude, $nav_row['category_id']); // Add to the exclusion list
          if ( $nav_row['category_id'] < 6 )
          { $top_level_on = $nav_row['category_id']; }

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

   function build_child($oldID) // Recursive function to get all of the children...unlimited depth
   {
       global $exclude, $depth; // Refer to the global array defined at the top of this script
       $tempTree = "";
       $child_query = mysql_query("SELECT * FROM `categories` WHERE parent_id=" . $oldID);
       while ( $child = mysql_fetch_array($child_query) )
       {
          if ( $child['category_id'] != $child['parent_id'] )
          {
             for ( $c=0;$c<$depth;$c++ ) // Indent over so that there is distinction between levels
             { $tempTree .= " "; }
             $tempTree .= "- " . $child['title'] . "<br>";
             $depth++; // Incriment depth b/c we're building this child's child tree (complicated yet???)
             $tempTree .= build_child($child['category_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['category_id']); // Add the item to the exclusion list
          }
       }
       return $tempTree; // Return the entire child tree
     }

     echo $tree;

?>
$c=0;
$x;
foreach($array1作为$key=>$val){
if(is_数组($val)){
echo“插入父id为$c的$key
”; $c++; $x=$c; foreach($valas$key1=>$val1){ 回显“插入父id为$x的$key1
”; $c++; if(is_数组($val1)){ foreach($val1作为$key2=>$val2){ 回显“插入父id为$c的$key2
”; $c++; getlevel($val2,$c); } }/*否则{ 回显“以其他方式插入父id为$c的$key1
”; $c++*/ } } } 函数getlevel($value$c1){ if(是_数组($value)){ foreach($keyV=>$value的值){ echo“插入父id为$c1的$keyV
”; $c1++; if(是_数组($Value)){ getlevel($Value$c1); } } } }

我曾经用你的sql写过echo replace。希望能有所帮助。这里
$array
是你的数组。

在任何深度级别树中的完美工作脚本下面

         $c=0;
    $x;
    foreach($array1 as $key=>$val){
        if(is_array($val)){
            echo "insert $key with parent id $c<br>";
            $c++;
            $x=$c; 
            foreach($val as $key1=>$val1){          
                echo " insert $key1 with parent id $x<br>";
   $c++;

                if(is_array($val1)){
                    foreach($val1 as $key2=>$val2){
                      echo "insert $key2 with parent id $c<br>";
                         $c++;
                        getlevel($val2,$c);
                    }
                }/* else{
                    echo "else insert $key1 with parent id $c<br>";
                    $c++; */
                }

            }

        }


    function getlevel($value,$c1){
        if(is_array($value)){
            foreach($value as $keyV=>$Value){
                echo " insert $keyV with parent id $c1<br>";
                $c1++;
                if(is_array($Value)){
                    getlevel($Value,$c1);
                }
            }
        }
    }

感谢大家的努力。

您可以从根递归遍历数组并保存所需内容,无论您提供的父id在给定的示例中以何种方式出现错误。感谢您的评论,但预期不会出现错误结果。您可以使用array\u walk\u recursive-您好,感谢您的快速回复,但我正在寻找能够将我上面的树数组保存到mysql中。您的脚本用于生成树。@VinodPatidar上面的代码是从DB生成树。您是否希望插入树?然后在每次插入时,您需要检查数据库中的父项(如果父项在那里),选择其id并在行中插入为父项id(如果父项不在那里),然后将其插入表中使用父id 0,它将成为父行,然后使用其id插入子行亲爱的,我这样做了,但没有成功。请为我编写脚本。我想在DB中插入树。这不是正确的脚本,它在深度级别失败。在这种情况下,检查此FYI失败:
$root_id;
$parent_id = 0;

foreach ( $tree_array as $root_key => $root_value ) {

    $parent_id = insertRecord($root_key, $parent_id);
    $root_id = $parent_id;

    if ( is_array($root_value) ) {
        foreach ( $root_value as $parent_key => $parent_value ) {

            $parent_id = insertRecord($parent_key, $root_id);
            $keep_parent_id = $parent_id;

            if ( is_array($parent_value) ) {
                foreach ( $parent_value as $child_key => $child_value ) {

                    $parent_id = insertRecord($child_key, $keep_parent_id);
                    getlevel($child_value, $parent_id);
                }
            }
        }
    }
}

function getlevel($sub_childs, $new_parent_id) {
    $keep_new_parent_id = $new_parent_id;

    if ( is_array($sub_childs) ) {
        foreach ( $sub_childs as $sub_child => $sub_child_sub ) {

            $new_parent_id = insertRecord($sub_child, $keep_new_parent_id);
            if ( is_array($sub_child_sub) ) {
                getlevel($sub_child_sub, $new_parent_id);
            }
        }
    }
}

function insertRecord($name, $parent_id) {
    $q = "insert into xtable set name = '".$name."',  parent_id = '".$parent_id."'";
    mysql_query($q);

    $folder_id = mysql_insert_id();

    return $folder_id;
}