如何将自相似的子对象(深层对象保存)保存到数据库中?

时间:2015-05-11 08:04:40

标签: php mysql ajax codeigniter

如何在数据库表中保存此数组,保留这种关系的任意数组之间的父子关系?

脚本:PHP 数据库:mysql

tbl_menu
===================
id, title, parent
================== 
Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [url] => http://google.cp,
            [text] => Contact Us
            [title] => undefined
            [childs] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 2
                            [url] => http://google.com
                            [text] => Contact - 1
                            [title] => undefined
                            [childs] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 3
                                            [url] => http://google.com
                                            [text] => Contact - 1 - 2
                                            [title] => undefined
                                        )

                                )

                        )

                )

        )

    [1] => stdClass Object
        (
            [id] => 4
            [url] => http://domain.com/
            [text] => About
            [title] => undefined
            [childs] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 5
                            [url] => http://google.com
                            [text] => About - 1
                            [title] => undefined
                        )

                    [1] => stdClass Object
                        (
                            [id] => 6
                            [url] => http://google.com
                            [text] => About - 2
                            [title] => undefined
                        )

                )

        )

    [2] => stdClass Object
        (
            [id] => 7
            [url] => /
        )

)
This is what i have done so far
    function save_menu()
    {
        $menu = $_POST['menu'];
        foreach($menu as $key => $item) 
        {
            $sql = "INSERT INTO tbl_menu (`title`,`parent`) values ('".$item['title']."','')";  
            mysql_query($sql);
        }
    }

2 个答案:

答案 0 :(得分:0)

function save_menu()
{
    $menu = $_POST['menu'];

        svae_data($menu)  
}

public function save_data($menu)
{

      foreach($menu as $key => $item) 
      {
           $sql = "INSERT INTO tbl_menu (`title`,`parent`) values  ('".$item['title']."','')";  
           mysql_query($sql);

           if(isset($item['childs']))
           {
                save_data($item['childs'])
           }
     }

}

答案 1 :(得分:0)

这是您想要的代码,但是请注意注入,并且自PHP 5.5以来mysql_函数已被弃用,因此使用PDO或mysqli。链接中的更多信息,请参见下文。

PHP PDO

mysql_real_escape_string

How can I prevent SQL injection in PHP?

function save_menu_recursive($menu, $parent = null) {
    if (!$parent) {
        $sql = "INSERT INTO tbl_menu(`title`) VALUES('{$menu->title}')";
    }
    else {
        $sql = "INSERT INTO tbl_menu(`title`, `parent`) VALUES('{$menu->title}', {$parent})";
    }
    mysql_query($sql);
    if (isset($menu->childs) && is_array($menu->childs)) {
        $insertedId = mysql_insert_id();
        foreach($menus->childs as $child) {
            save_menu_recursive($child, $insertedId);
        }
    }
}