Jump to content


Photo

Delete multiple entries w/ Checkboxes


  • Please log in to reply
3 replies to this topic

#1 Zoey

Zoey
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 18 March 2006 - 07:59 PM

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

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

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

else if($row_del) { 
    foreach ($_POST as $id => $value) {
          $query = "DELETE FROM polls WHERE id=$row_id"; }

And here's the code used to make the checkboxes that allow me to edit/delete a suggestion.

$maindiv.= "<input type='checkbox' name='id_".$row['id']."'>";

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

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 18 March 2006 - 11:57 PM

You are almost there!!!!

now....

your checkbox...use it lie this instead.

$maindiv.= '<input type="checkbox" name="id[" . $row['id'] . "]">';

then in the code that deletes these commenst do this....

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

Thats about as elegant as you will get it.
follow me on twitter @PHPsycho

#3 Zoey

Zoey
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 19 March 2006 - 12:49 AM

Aha! I put together your help, and help from another forum and got it!

Thanks so much!

#4 Zoey

Zoey
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 19 March 2006 - 03:29 AM

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

$maindiv.= "<input type='checkbox' name='ids[]' value='".$row['id']."'>";

And to delete multple comments is:

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

But now its not letting me edit comments.

I think it has something to do with the remaining:

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

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users