Zoey Posted March 18, 2006 Share Posted March 18, 2006 I've got this page in which people can input their suggestions, and the information is stored on my server. To deal with these suggestions, I created a page (with the help of my friend Bill) which allows me to delete/edit suggestions. However, currently, the page will only delete one suggestion at a time.. but I'd like it to be able to delete all of the suggestions that are checked. Here's the code for the admin actions[code] $row_del = (isset($_POST['sub_del'])) ? $_POST['sub_del'] : 0; if(isset($_POST['sub_del']) || isset($_POST['sub_edit'])) { foreach($_POST as $key => $value) { if ( strpos($key, "id_") !== false ) { $row_id = str_replace("id_","",$key); if ($row_id > 0) break; } } [/code]sub_del is the button you click to delete entries.. sub_edit is the name of the button you click to edit entries.Here's the code that actually deletes the entries[code]else if($row_del) { foreach ($_POST as $id => $value) { $query = "DELETE FROM polls WHERE id=$row_id"; }[/code]And here's the code used to make the checkboxes that allow me to edit/delete a suggestion.[code]$maindiv.= "<input type='checkbox' name='id_".$row['id']."'>";[/code]Does anyone have any suggestions on how I can get the script to allow multiple suggestions to be deleted at once? Thanks.(If you need me to post more of the coding, just let me know.. I only posted what I considered the important snipits, but since this code isn't entirely mine, there could be more that I'm missing... thanks). Quote Link to comment https://forums.phpfreaks.com/topic/5246-delete-multiple-entries-w-checkboxes/ Share on other sites More sharing options...
ToonMariner Posted March 18, 2006 Share Posted March 18, 2006 You are almost there!!!!now....your checkbox...use it lie this instead.[code]$maindiv.= '<input type="checkbox" name="id[" . $row['id'] . "]">';[/code]then in the code that deletes these commenst do this....[code]// create array of comments to delete.$i = 0;foreach ($_POST['id'] as $key => $value { // loop through all the post vars id $delarr .= ($i ==0) ? $value : "," . $value; // create a comma separated list of comment id's to delete. $i = 1;}// Once you have the list just do this...$query = "DELETE FROM polls WHERE id IN (" . $delarr . ")";// echo $query; // remove the comment obefore the echo to check the query you will run.// $query = mysql_query($query); // remove the comment at the start of this line when you are happy.[/code]Thats about as elegant as you will get it. Quote Link to comment https://forums.phpfreaks.com/topic/5246-delete-multiple-entries-w-checkboxes/#findComment-18677 Share on other sites More sharing options...
Zoey Posted March 19, 2006 Author Share Posted March 19, 2006 Aha! I put together your help, and help from another forum and got it!Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/5246-delete-multiple-entries-w-checkboxes/#findComment-18694 Share on other sites More sharing options...
Zoey Posted March 19, 2006 Author Share Posted March 19, 2006 Ack, ok nevermind. I thought I had it, but for some reason I'm having another problem. I changed the code so that the checkbox code is[code]$maindiv.= "<input type='checkbox' name='ids[]' value='".$row['id']."'>";[/code]And to delete multple comments is:[code]else if($row_del) { $query = "DELETE FROM polls WHERE"; $j=0; foreach($_POST['ids'] as $key=>$id) { if($j>0) { $query .= " OR"; } $query .= " id='".$id."'"; $j++; } }[/code]But now its not letting me edit comments.I think it has something to do with the remaining:[code] if(isset($_POST['sub_del']) || isset($_POST['sub_edit'])) { foreach($_POST as $key => $value) { if ( strpos($key, "id_") !== false ) { $row_id = str_replace("id_","",$key); if ($row_id > 0) break; } } } [/code]since the checkbox is no longer called id_.. but no matter what combination of ids[] I try to change the id_ to, its still not allowing me to edit my comments.Any idea what I should fix the id_ to? Quote Link to comment https://forums.phpfreaks.com/topic/5246-delete-multiple-entries-w-checkboxes/#findComment-18723 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.