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);
}
}