sandersomers Posted August 17, 2006 Share Posted August 17, 2006 Hello fellow php scripters,I am new to this forum, so please be easy on me.I don't have much experience, but i like to tell myself i do. ;)I made a database driven navigation menu, or a folder structure menu... whatever you would like to call it.it's structure is quit easy.CREATE TABLE `file_manager` ( `id` int(11) NOT NULL auto_increment, `menu_id` int(11) NOT NULL, `menu_name` varchar(128) collate latin1_general_ci NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=138 ;So let's say i have 5 menu items and all items have several sub items.. I want to delete a item with sub items, here i get stuck with my loop. $result = mysql_query("SELECT * FROM article_menu WHERE id='$is_the_first_item_to_delete'"); while($row = mysql_fetch_array($result)){ $result_sub = mysql_query("SELECT * FROM article_menu WHERE id='$row[menu_id]'"); while($row_sub = mysql_fetch_array($result_sub)){ mysql_query("DELETE FROM article_menu WHERE menu_id='$row_sub[id]'"); } mysql_query("DELETE FROM article_menu WHERE menu_id='$is_the_first_item_to_delete'");}This will only delete the first sub items, however i would like to have all sub items to be deleted.I have searched this site but i couldn't find anything useful. This could be caused by my searching skills.Hopefully one of you scripters could help me out. ??? Quote Link to comment https://forums.phpfreaks.com/topic/17819-mysql-delete-loop/ Share on other sites More sharing options...
Woolf Posted August 19, 2006 Share Posted August 19, 2006 From what you've stated, this should, in theory, should work:[code]<?php$query = mysql_query("SELECT menu_id FROM article_menu WHERE id='$is_the_first_item_to_delete'") or die ('Aye, I think we have a problem!');$r = mysql_fetch_array($query) //Results from query$menu_id = $r['menu_id']; //The menu id//Delete all items where menu_id is $menu_idmysql_query("DELETE FROM article_menu WHERE menu_id='$menu_id'");?>[/code]This would essentially delete all items with the menu_id of x, where x is the value of $menu_id. Quote Link to comment https://forums.phpfreaks.com/topic/17819-mysql-delete-loop/#findComment-77078 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.