vchris 3 Posted August 27, 2006 Report 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 Quote Link to post Share on other sites
skym 0 Posted August 27, 2006 Report 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. Quote Link to post Share on other sites
vchris 3 Posted August 27, 2006 Author Report 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? Quote Link to post Share on other sites
aleksanteri 0 Posted August 27, 2006 Report Share Posted August 27, 2006 You need to do a foreach loop Quote Link to post Share on other sites
reportingsjr 4 Posted August 27, 2006 Report 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 Quote Link to post Share on other sites
vchris 3 Posted August 27, 2006 Author Report 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)? Quote Link to post Share on other sites
skym 0 Posted August 28, 2006 Report 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.")";... Quote Link to post Share on other sites
vchris 3 Posted August 28, 2006 Author Report 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)? Quote Link to post Share on other sites
skym 0 Posted August 28, 2006 Report Share Posted August 28, 2006 Check my code again. Quote Link to post Share on other sites
vchris 3 Posted August 28, 2006 Author Report Share Posted August 28, 2006 ahhh! .","; Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 28, 2006 Report 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.")"; ...} Quote Link to post Share on other sites
vchris 3 Posted August 28, 2006 Author Report Share Posted August 28, 2006 This $str = implode(",", $_POST['itemid']); will add commas between each value right? Quote Link to post Share on other sites
justsomeguy 1,135 Posted August 28, 2006 Report 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. Quote Link to post Share on other sites
vchris 3 Posted August 28, 2006 Author Report Share Posted August 28, 2006 Thanks Quote Link to post Share on other sites
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.