Jump to content

how would I Delete all thats checked


TeddyKiller

Recommended Posts

I have a messages inbox and sentbox (working on inbox first) with a check all and uncheck all feature.

I want to know how I'd delete all thats checked.

Here is my code for the forms or whatever.

 

      default:
          echo '<a href="mail.php?act=send">Send Message</a> - <a href="mail.php?act=sent">Sent Messages</a><br />';
          echo "<fieldset><legend>Inbox Messages</legend><form name=\"myForm\" action=\"\" onSubmit=\"return ValidateForm(this,'chkSample')\">";
          
          $q = mysql_query("SELECT * FROM `sent_messages` WHERE to_status='0' AND to_id='$user->id'");
          while($r = mysql_fetch_array($q)){
              $wr = mysql_query("SELECT * FROM `users` WHERE id='".$r['from_id']."'");
              $rw = mysql_fetch_array($wr);
              echo '
              <div style="text-align:left; border-bottom:1px solid #e1e3d3; padding-bottom:5px;">
              <div id="topmail">From: <a href="#">'.ucwords($rw['username']).'</a> - Subject: <a href="#">'.ucwords($r['subject']).'</a></div>
              <div id="mail">
              <div style="float:right; width:30px; padding-left:10px; padding-right:10px;">
              <input name="chkSample" type="checkbox" value="" />
              </div>
              <div style="margin-right:50px;">'.substr($r['message'],0,100).'</div>
              </div>
              </div><br />';
          }
          
          echo '</fieldset><br />
          <div style="text-align:right;">
          <a href="javascript:SetChecked(1,\'chkSample\')">Check All</a> - 
          <a href="javascript:SetChecked(0,\'chkSample\')">Uncheck All</a>
          </div></form><br /><br />';
          
      break;

 

How would I delete all thats checked? Thanks

Link to comment
Share on other sites

In order to delete all the checked rows, you need to know which items are checked. This is your current checkbox (which is the same for every message because it's used in the while loop):

 

<input name="chkSample" type="checkbox" value="" />

 

The problem here is that every checkbox has the same name, and has no value. In order to be able to distinguish which checkboxes are checked, I'd recommend changing your checkbox code to this:

 

<input name="checkedItems[]" type="checkbox" value="'.$r['unique_database_id'].'" />

 

Obviously you need to change 'unique_database_id' to whatever the column that identifies each message uniquely is.

 

The list of checked items will be returned as an array in $_POST like so:

 

foreach ($_POST['checkedItems'] as $value) {
    echo $value; // Echoes the unique ID of each checked message
}

Link to comment
Share on other sites

I understand what your saying. I've got that setup. May be dumb, but whats next?

foreach ($_POST['checkedItems'] as $value) {
    //Would I run my php query in here? This will loop for all thts checked, right?
}

 

Also.. but how would I call it becuse..I don't want a button, I would like a link?

Link to comment
Share on other sites

No a far better approach then the foreach () would be:

 

$in = implode(',', $_POST['checkedItems']);// I recommend that you verify that 1) the user is allowed to do this 2) it's an array containing only integers
$query = "DELETE FROM table WHERE id IN ($in)";
$result = mysql_query($query);

Link to comment
Share on other sites

No a far better approach then the foreach () would be:

 

$in = implode(',', $_POST['checkedItems']);// I recommend that you verify that 1) the user is allowed to do this 2) it's an array containing only integers
$query = "DELETE FROM table WHERE id IN ($in)";
$result = mysql_query($query);

 

Is this a loop? and yes, it uses integers, also wouldn't $in be - ,13

(If the ID is 13 that is)

Link to comment
Share on other sites

(The above solution is a lot more elegant - and probably more efficient too - than mine, I have some comments about using a link instead of a button below though)

 

How to delete the items, specifically, is going to depend on your database (of course) but I'll do my best to give you something generic (written for MySQL, since that's what I use) you can tailor to your use. Make sure that before you do delete anything, the user requesting the delete actually has permission to delete each of the files requested.

 

There's nothing to stop a user from building a POST or GET request with any message ID they want to put in (even if the messages don't belong to them) so plan accordingly!

 

$delete = 'DELETE FROM your_messages_table_name WHERE ';
$start = TRUE;
$i=0;
foreach ($_POST['checkedItems'] as $value) {
    if (!$start)
        $delete .= ' OR ';
    else
        $start = FALSE;
    $delete .= 'message_id_column = '.$value;
    $i++
}
$delete .= ' LIMIT '.$i;

// Delete is now ready to run on your server, but you should make sure
// that the user actually has permission to delete each of the ID's they're trying to delete!

 

As for the issue of using a link instead of a button. This is a javascript issue really. There's a few ways you could do this. I'd recommend this...

 

<input name="checkedItems[]" id="message_check_'.$r['unique_database_id'].'" type="checkbox" value="'.$r['unique_database_id'].'" />

 

Have your delete url look something like...

 

<a onClick="deleteMessages()" style="cursor:pointer;">Delete Checked Messages</a>

(A quick note, cursor:pointer may not work on IE, you may have to use cursor:hand in addition to cursor:pointer, or another style).

 

(I'm suddenly getting really lazy because I've been writing this post for about 10-15 minutes, so here's a quick note about what you need to do, and if you need specific examples than hopefully someone can help you, or I can continue later).

 

In your loop where you pull each message row, you need to create a series of if then javascript statements that checks each individual checkbox (they each have unique ID's, so you can pull their ID's using getElementById) to see if it is checked or unchecked (and if it's checked, to build a string that will take the user to a delete page and sends the ID's in checkedItems[], the request can be POST or GET, just change the PHP above from $_POST to $_GET if you switch to GET).

 

Then outside your loop, you have to put that javascript statement you made into the deleteMessages javascript function that builds the GET or POST request and sends it for the user.

Link to comment
Share on other sites

No a far better approach then the foreach () would be:

 

$in = implode(',', $_POST['checkedItems']);// I recommend that you verify that 1) the user is allowed to do this 2) it's an array containing only integers
$query = "DELETE FROM table WHERE id IN ($in)";
$result = mysql_query($query);

 

Is this a loop? and yes, it uses integers, also wouldn't $in be - ,13

(If the ID is 13 that is)

 

No this is not a loop it uses the IN operator to delete all rows with the corresponding ID's (=integers provided by $_POST['checkedItems'])

 

And if you pass 13 it will show up as 13 if you pass 12 and 13 it will show up as 12, 13

Link to comment
Share on other sites

This will partly depend on how you send the delete request, but if done correctly, $_POST['checkedItems'] will be unset if nothing is checked.

 

In other words:

 

if (isset($_POST['checkedItems'])) {
    // At least 1 item checked
}
else {
    // Nothing checked
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.