php - How save data to database using recursion -
my mysql table looks this:
page_id parnt_id page_name ------------------------------------ 1 0 aboutus 2 1 contact_us 3 2 home 4 2 currear
and have array looks this:
array ( [0] => array ( [id] => 1 [children] => array ( [0] => array ( [id] => 2 [children] => array ( [0] => array ( [id] => 3 ) ) ) ) ) )
how can add above array database using recursion?
$all_pages = $given_array; function save_page($page, $parent_id = 0) { $page_id = $page['id']; $sql = "insert table_name (page_id, parent_id) values ($page_id, $parent_id)"; mysql_query($sql); if(array_key_exists('children', $page)) { save_page($page['children'], $page['id']); } } save_page($all_pages);
Comments
Post a Comment