say i have 5 emails stored in a DB I set up it to echo out all the emails, and have the checkbox, but how do I get the check boxes to delete the selected emails?
Here an example. if (is_array($_POST['email'])) { mysql_query("DELETE FROM mails WHERE mailid IN(". implode(', ' array_map('intval', $_POST['email'])) .")"); } PHP: And put something like this in the loop when receiving the mails from the database. <input type="checkbox" name="email[]" value="<?php echo $row['emailid']; ?>" /> HTML:
Use a form with the checkboxes and post it to nico_swd's PHP code. What part did you not understand weknowtheworld?
if (is_array($_POST['email'])) { mysql_query("DELETE FROM mails WHERE mailid IN(". implode(', ' . array_map('intval', $_POST['email'])) .")"); } PHP: Here is nico_swd's code fixed up.. Pesky "."s haha Cheers,
I got it to work using if (isset($_POST[email])) { foreach($_POST as $k => $v){ $$k = $v; } foreach($email as $v){ mysql_query("DELETE FROM mail where mailid='$v'"); } } PHP: I couldn't get it to work your way Can anyone explain the above code? I looked up foreach on php.net, but it made no sense to me. WHy do i need $k (i am assuming i can use any other variable, but still it seems roundabout to do it that way)
If you want to do it that way, it should look more like this: if (isset($_POST['email'])) { foreach($_POST['email'] as $key => $value){ mysql_query("DELETE FROM mail where mailid='" . $value . "'"); } } PHP: Basically what will happen: Foreach looks at an array of objects and goes through each one. The "$key" is the value in the square brackets ([], usually something like $email[0] $email[1])... it identifies the current value and position of the search. The "$value" is the data (so for your checkbox, it would be the ID) .. this is what we want. Foreach will run through each object in the array and return it as variables.. this case $key and $value. To delete the ID just run mysql_query("DELETE FROM mail WHERE mailid='" . $value . "'"); PHP: And it will delete that ID on the current passthrough. 'mail' is the table name, 'mailid' is the match we're going to perform. Hope this helps. Cheers, (btw the above will save a few lines of code, and may be a little easier to understand).
The array basically looks like this $email = array ( key1 => value1, key2 => value2 ); PHP: $email[key1] = value1 & $email[key2] = value2 Code (markup): When running foreach ($email as $key => $value) this happens: 1st pass through $key is set as key1 $value is set as value1 2nd pass through $key is set as key2 $value is set as value1 Code (markup): The thing with the foreach vs nico's code, is that you're running a query every loop through. So if you have 5 things to delete, you have to run 5 separate queries. This can slow down execution time (maybe not as important in an admin panel, or personal project... but in a live environment, the less queries the better). We're taking 2/100ths of a second, so it probably wont be noticed. Edit, something that may help is to create an array in php, and then run print_r($createdArray); where $createdArray is your array value. This will let you see it visually.
ok, i tried nico's method (well your update to it) and got: Warning: implode() [function.implode]: Argument to implode must be an array. in...
Oh.. woops. if (is_array($_POST['email'])) { mysql_query("DELETE FROM mails WHERE mailid IN(". implode(', ', array_map('intval', $_POST['email'])) .")"); } PHP: Sorry, that should be a comma not a period Cheers,