vchris Posted August 27, 2006 Share Posted August 27, 2006 I am working on a project and each record has it's own checkbox. These checkbox are to delete the selected record. You can have 1 record like you can have all records (could be hundreds). I'm not sure how to get every value of every checkbox and I'm not sure of the delete mysql query for those records.I tried this: <input type=\"checkbox\" name=\"itemdel\" value=\"" . $row['linkid'] . "\"> Which only display 1 number.I also tried this: <input type=\"checkbox\" name=\"itemdel[]\" value=\"" . $row['linkid'] . "\"> Which only display the word "Array".My display statement: echo $_POST['itemdel']; I basically only need a list of selected values and need someway to work with it.Thanks Link to comment Share on other sites More sharing options...
skym Posted August 27, 2006 Share Posted August 27, 2006 With name="itemdel" you will get the value of the last checked box.With name="itemdel[]" you get an array with all the values of the checked boxes. Just var_dump($_POST['itemdel']); and you'll see them. Link to comment Share on other sites More sharing options...
vchris Posted August 27, 2006 Author Share Posted August 27, 2006 This is what is displayed: array(2) { [0]=> string(1) "7" [1]=> string(1) "5" } Could I do this? $query = "DELETE FROM mf_links WHERE linkid IN (" . var_dump($_POST['itemdel[]']) . ")"; Or would I have to create a loop? Link to comment Share on other sites More sharing options...
aleksanteri Posted August 27, 2006 Share Posted August 27, 2006 You need to do a foreach loop Link to comment Share on other sites More sharing options...
reportingsjr Posted August 27, 2006 Share Posted August 27, 2006 You would need to use a foreach, and if you dont know how to check if a checkbox is checked here is how: if($_POST[$foreachnum] == true){//delete from table} Im sure you already knew that though Link to comment Share on other sites More sharing options...
vchris Posted August 27, 2006 Author Share Posted August 27, 2006 So I have to create a query for each record to be deleted?I can't just do a WHERE linkid IN (1, 2, 3, 4)? Link to comment Share on other sites More sharing options...
skym Posted August 28, 2006 Share Posted August 28, 2006 You can use foreach to make the string of those numbers: $str = "";foreach($_POST['idemid'] as $value) {$str .= (int)$value.",";}$str = substr($str,0,-1);$query = "DELETE FROM mf_links WHERE itemid(".$str.")";... Link to comment Share on other sites More sharing options...
vchris Posted August 28, 2006 Author Share Posted August 28, 2006 hmmm interesting... but this will only delete 1 record won't it? Is it possible to have those values separated with commas so I can do WHERE linkid IN (1,2,3)? Link to comment Share on other sites More sharing options...
skym Posted August 28, 2006 Share Posted August 28, 2006 Check my code again. Link to comment Share on other sites More sharing options...
vchris Posted August 28, 2006 Author Share Posted August 28, 2006 ahhh! .","; Link to comment Share on other sites More sharing options...
justsomeguy Posted August 28, 2006 Share Posted August 28, 2006 You can also just join or implode the array and join it on commas: if (count($_POST['itemid']) > 0){ $str = implode(",", $_POST['itemid']); $query = "DELETE FROM mf_links WHERE itemid IN (".$str.")"; ...} Link to comment Share on other sites More sharing options...
vchris Posted August 28, 2006 Author Share Posted August 28, 2006 This $str = implode(",", $_POST['itemid']); will add commas between each value right? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 28, 2006 Share Posted August 28, 2006 Right, implode takes an array of elements, and returns a string that is the list of elements joined by whatever string you give it (in this case, a comma). If there is only one element, it will just return that element. Link to comment Share on other sites More sharing options...
vchris Posted August 28, 2006 Author Share Posted August 28, 2006 Thanks Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now