coolphpdude Posted June 10, 2008 Share Posted June 10, 2008 Hi there, i think this will be an easy one for you lot but i just can't seem to get my head around it. I got a message database on my project. So a user receives messages from other user's just like an email system. Currently i have a delete button next to each message as it is output from the database using a while loop. Now the propblem with this is, if you have got 40 messages you have to click delete 40 times. I want it so that theres a tick box next to each message so u can mark the messages you would like to delete and click a single delete button at the top to delete all of the marked messages. Is this easy? most email messaging systems have it so i suppose its do able, i just dont know how. Also could i place a tick box at the so the user could click this and it would mark all messages on this page?? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/ Share on other sites More sharing options...
jesushax Posted June 10, 2008 Share Posted June 10, 2008 this should help... switch(@$_GET["action"]) { case "delete"; <?php if(is_array($_POST['checkbox'])){ foreach($_POST['checkbox'] as $'recordID'){ delete mysql here } } break; default: html boxes in yoru loop <form action="?action=delete"> <input class=\"blank\" name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$row['recordID']." \"> break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-561913 Share on other sites More sharing options...
coolphpdude Posted June 10, 2008 Author Share Posted June 10, 2008 i cant quite understand that, could you explain it? Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-561919 Share on other sites More sharing options...
jesushax Posted June 10, 2008 Share Posted June 10, 2008 well in your loop of the records add echo "<input class="blank\" name=\"checkbox[]\" type=\"checkbox\" id=\"checkbox[]\" value=\"".$row['recordID']." \">" this will put a checkbox next to each of the rows, the value of these checkboxes must be set to the vlaue of the the record id then have all of yoru records inside a form with the action set to "?action=delte" you may want to add a php self script in here then using the case slected method switch(@$_GET["action"]) { case "delete"; <?php if(is_array($_POST['checkbox'])){ foreach($_POST['checkbox'] as $'recordID'){ mysql_query("Delete From tblNAME WHERE ID='".$'recordID'."'") or die(mysql_error()); } } break; the code will loop through all checkboxes that are ticked take the ids from them and delete them Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-561938 Share on other sites More sharing options...
coolphpdude Posted June 10, 2008 Author Share Posted June 10, 2008 So would i put a delete button at the top hyperlinked to say delete.php and in that script include... switch(@$_GET["action"]) { case "delete"; <?php if(is_array($_POST['checkbox'])){ foreach($_POST['checkbox'] as $'recordID'){ mysql_query("Delete From tblNAME WHERE ID='".$'recordID'."'") or die(mysql_error()); } } break; ...is that right? Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-561944 Share on other sites More sharing options...
jesushax Posted June 10, 2008 Share Posted June 10, 2008 no you would put a deleted button after all the records then have no even for it the event is in the form Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-561948 Share on other sites More sharing options...
coolphpdude Posted June 10, 2008 Author Share Posted June 10, 2008 cheers i'll give it a go now! Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-561959 Share on other sites More sharing options...
coolphpdude Posted June 11, 2008 Author Share Posted June 11, 2008 Bump! Ive put the checkbox in for every message output in my while loop. I don't quite understand what the lad above is saying (although i do really appreciate your help). Im not sure what i shoudl be setting as the attributes on his line of code or what to do to create the delete button/link that wil delete the selected messages. Can anyone help?? Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-562986 Share on other sites More sharing options...
jesushax Posted June 11, 2008 Share Posted June 11, 2008 post all your code thus far... Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-562988 Share on other sites More sharing options...
coolphpdude Posted June 11, 2008 Author Share Posted June 11, 2008 echo "<table>\n"; echo "<tr><th>Message ID</th><th>Subject</th><th>Delete</th></tr>\n"; while ($rows = mysql_fetch_array($messages)) { printf("<tr><td align='center' width='100'><font face='arial'><font color='#648CC8'>%s</font></td>\n", $row["id"]); printf("<td>%s</td>\n", $row["subject"]); printf("<td align='center' cellpadding='10' width='60'><input class='blank' name='checkbox[]' type='checkbox' id='checkbox[]' value='".$row['recordID']." '></td></tr>"); } } echo "</table>\n"; and now im not sure where to put the delete or what to set Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563001 Share on other sites More sharing options...
jesushax Posted June 11, 2008 Share Posted June 11, 2008 here you go have a look at what ive added and you should be able to figure out how it works <?php switch(@$_GET["action"]) { Case "delete": if(is_array($_POST['checkbox'])){ foreach($_POST['checkbox'] as $RecordID){ mysql_query("DELETE FROM tablename WHERE RecordID='$RecordID'") or die(mysql_error()); } } echo 'records delteed'; break; default: echo '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=delete">'."\n"; echo "<table>\n"; echo "<tr><th>Message ID</th><th>Subject</th><th>Delete</th></tr>\n"; while ($rows = mysql_fetch_array($messages)) { printf("<tr><td align='center' width='100'><font face='arial'><font color='#648CC8'>%s</font></td>\n", $row["id"]); printf("<td>%s</td>\n", $row["subject"]); printf("<td align='center' cellpadding='10' width='60'><input class='blank' name='checkbox[]' type='checkbox' id='checkbox[]' value='".$row['recordID']." '></td></tr>"); } } echo '<tr><td colspan="3"><input type="submit" value="Delete these records" />'."\n"; echo "</table>\n"; echo "</form>\n"; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563010 Share on other sites More sharing options...
coolphpdude Posted June 11, 2008 Author Share Posted June 11, 2008 cheers i'll try it Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563013 Share on other sites More sharing options...
coolphpdude Posted June 11, 2008 Author Share Posted June 11, 2008 echo "<form method='post' action=''.$_SERVER['PHP_SELF'].'?action=delete'>'."; Can someone help me sort out the " and ' on this please? Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563066 Share on other sites More sharing options...
jesushax Posted June 11, 2008 Share Posted June 11, 2008 this is right echo '<form method="post" action='''.$_SERVER['PHP_SELF'].'?action=delete">'; or echo "<form method=\"post\" action=\"".$_SERVER['PHP_SELF']."?action=delete\">"; Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563068 Share on other sites More sharing options...
coolphpdude Posted June 11, 2008 Author Share Posted June 11, 2008 Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in C:\Inetpub\vhosts\thesellerswarehouse.co.uk\httpdocs\mymessages.php on line 172 i get that error Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563070 Share on other sites More sharing options...
jesushax Posted June 11, 2008 Share Posted June 11, 2008 whats line 172 and 171? Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563076 Share on other sites More sharing options...
coolphpdude Posted June 11, 2008 Author Share Posted June 11, 2008 echo "<br>"; echo '<form method="post" action='''.$_SERVER['PHP_SELF'].'?action=delete">'; echo "<table width='100%' border='2' bordercolor='#cccccc' style='border-collapse: collapse' cellpadding='5' align=center >\n"; they are line 170-173 Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563078 Share on other sites More sharing options...
jesushax Posted June 11, 2008 Share Posted June 11, 2008 this instead next to you acttion you had ''' needed to be "' echo "<br>"; echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'?action=delete">'; echo "<table width='100%' border='2' bordercolor='#cccccc' style='border-collapse: collapse' cellpadding='5' align=center >\n"; Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563089 Share on other sites More sharing options...
coolphpdude Posted June 11, 2008 Author Share Posted June 11, 2008 that works, im now seeing the tick box, however its not deleteing. I'll have a play about with it when i get back. Cheers for your help, much appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563127 Share on other sites More sharing options...
jesushax Posted June 11, 2008 Share Posted June 11, 2008 well where this code is swap tablename for you table name if you havent already done does the mysql output any errors when you press the submit button? Case "delete": if(is_array($_POST['checkbox'])){ foreach($_POST['checkbox'] as $RecordID){ mysql_query("DELETE FROM tablename WHERE RecordID='$RecordID'") or die(mysql_error()); } } echo 'records delteed'; break; Quote Link to comment https://forums.phpfreaks.com/topic/109543-select-all-then-delete/#findComment-563135 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.