Jump to content


Photo

MYSQL delete loop


  • Please log in to reply
1 reply to this topic

#1 sandersomers

sandersomers
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 17 August 2006 - 07:58 AM

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. ???

#2 Woolf

Woolf
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 19 August 2006 - 12:46 AM

From what you've stated, this should, in theory, should work:

<?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_id
mysql_query("DELETE FROM article_menu WHERE menu_id='$menu_id'");
?>

This would essentially delete all items with the menu_id of x, where x is the value of $menu_id.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users