Shadowing Posted December 26, 2011 Share Posted December 26, 2011 I really need some huge help on deleting rows with check boxes. Im completly lost on how to do this. Read some examples online and even with the code being small to do this i cant figure it out on intergrading it with the code I wrote. sorry that the code is long on this. I have decent comments though. At the very bottom i have started writing the delete code. My problem is figuring out how to make the script know what check box is checked. I have a select all check box java script on this page too <form name="frm1" id="frm1" action="" method="post" 15.onsubmit="javascript:return submitIt('frm1')"> <p> <center> <table width="60%" border="1" cellspacing="1" cellpadding="0"> <tr align="center"> </tr> <tr align="center"> <td><input type="checkbox" name="checkAll" value="x" id="checkall" onclick="checkUncheckAll('association[]', this.checked)" /></td> <td>Recipient</td> <td>Subject</td> <td>Sent</td> <td>read/unread</td> </tr> <?php // find out how many rows are in the table $count3 = "SELECT COUNT(*) FROM sent WHERE id = '".($_SESSION['user_id'])."'";; $count2 = mysql_query($count3) or trigger_error("SQL", E_USER_ERROR); $r = mysql_fetch_row($count2); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 10; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['sent']) && is_numeric($_GET['sent'])) { // cast var as int $currentpage = (int) $_GET['sent']; } else { // default page num $currentpage = 1; }// end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; }// end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $count3 = "SELECT * FROM sent WHERE id = '".($_SESSION['user_id'])."' ORDER BY time DESC LIMIT $offset, $rowsperpage "; $count2 = mysql_query($count3) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($count2)) { // these are variables to place in the echo $sent_id = $list['sent_id']; $recipient = $list['sendto']; $subject = $list['subject']; $read = $list['read']; // grabs the time offset $take3 = "SELECT time_offset FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'"; $take2 = mysql_query($take3) or die(mysql_error()); $take1 = mysql_fetch_array($take2); $recieved = $list['time']; // adds the users time offset to the inputed time $time = ($recieved + $take1['time_offset']); // sets default time to UTC then formats the inputed time that was offset with the users offset date_default_timezone_set('UTC'); $user_offset = date('h:i:s a M/d', $time); ?> <tr> <td><center><input type="checkbox" name="association[]" id="<?php echo $sent_id; ?>" value="1" /></center></td> </label></td> <td><center><a href="search_goaulds.php?goauld=<?php echo $recipient; ?>"><?php echo $recipient ?></a></center></td> <td><center><a href="sent_view.php?sent_id=<?php echo $sent_id; ?>"><?php echo $subject ?></a></center></td> <td><center><?php echo $read ?></center></td> <td><center><?php echo $user_offset ?></center></td> </tr> <?php } // while loop ?> </table> </center> <div class="deletecontainer"> <div class="delete"> <br/> <input type="submit" name="delete" id="delete" value="Delete"></p> </form> </div> </div> <?php if(isset($_POST['delete'])) { // if checkbox is checked with the matching sent_id if(isset($_POST['sent_id'])) { // then delete sent_id matching the checkbox id mysql_query("DELETE FROM `sent` WHERE `sent_id`= '".($_POST['sent_id'])."'"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253837-deleting-rows-with-check-boxes/ Share on other sites More sharing options...
kicken Posted December 26, 2011 Share Posted December 26, 2011 My problem is figuring out how to make the script know what check box is checked. Only the boxes that are checked are submitted, so it's as simple as looping over your array of boxes. foreach ($_POST['association'] as $sent_id){ //do something } Since what you want to do is delete them, you could do it all in one query by just using implode() to get a comma separated list of ID's and use that in your query. I would use array_map + intval to ensure they are all integers first to protected against sql injection. Quote Link to comment https://forums.phpfreaks.com/topic/253837-deleting-rows-with-check-boxes/#findComment-1301370 Share on other sites More sharing options...
Shadowing Posted December 26, 2011 Author Share Posted December 26, 2011 thanks for responding kicken im reading tizag.com implode tutor lol I think im starting to understand this please let me know if im getting this idk can i just shove $glue in the query like that thats the whole point in impoloding right to beable to put it all into a single variable? <?php if(isset($_POST['delete'])) { $pieces = array("$sent_id"); // is that how i grab them? $glue = implode("-", $pieces); foreach ($_POST['association'] as $sent_id){ mysql_query("DELETE FROM `sent` WHERE `sent_id`= '".mysql_real_escape_string($glue)."'"); } } >? Quote Link to comment https://forums.phpfreaks.com/topic/253837-deleting-rows-with-check-boxes/#findComment-1301438 Share on other sites More sharing options...
Shadowing Posted December 26, 2011 Author Share Posted December 26, 2011 Alright tested this out and its deleting only one at a time and doesnt even delete the one I clicked on lol its deleting the very last one so if i have 2 pages of rows it deletes the very last one on the 2nd pagination page. so im doing something wrong here Quote Link to comment https://forums.phpfreaks.com/topic/253837-deleting-rows-with-check-boxes/#findComment-1301470 Share on other sites More sharing options...
Shadowing Posted December 27, 2011 Author Share Posted December 27, 2011 im completly stuck on this. I have extinguished all ideas Quote Link to comment https://forums.phpfreaks.com/topic/253837-deleting-rows-with-check-boxes/#findComment-1301663 Share on other sites More sharing options...
Shadowing Posted December 28, 2011 Author Share Posted December 28, 2011 after massive research lol I solved my problem with <?php // deletes code for check boxes if(isset($_POST['delete'])) { // only if a box is checked if (!empty($_POST['association'])) { // loops through and grabs the sent_id of each box that is checked foreach($_POST['association'] AS $sent_id => $val) { $sent_id = (int) $val; // deletes the message related to the sent_id of the check box mysql_query('DELETE FROM `sent` WHERE `sent_id`= ' . (int) $sent_id); header("Location: sent.php?sent=1"); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/253837-deleting-rows-with-check-boxes/#findComment-1301783 Share on other sites More sharing options...
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.