0

How do I save this array in my database table, preserving the parent-child relationship over an arbitrary array of such relationships?

Script: PHP Database: 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);
        }
    }
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Mohit Singh
  • 89
  • 1
  • 8

2 Answers2

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'])
           }
     }

}
Deep Hakani
  • 197
  • 1
  • 8
0

This is the code which You've wanted, but please pay attention to injections, and the mysql_ functions are deprecated since php 5.5 so use PDO or mysqli. More info in links, se below.

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);
        }
    }
}
Community
  • 1
  • 1
Vahe Shadunts
  • 1,956
  • 14
  • 18