SkyRanger Posted May 19, 2011 Share Posted May 19, 2011 Not sure if this is possible I am trying to do a multi delete: <form name="form1" method="post" action=""> <table> while($rows=mysql_fetch_array($result)){ ?> <tr> <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td> <td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['name']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['lastname']; ?></td> <td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td> </tr> <?php } ?> // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } <tr><td> <select name="dropdown"> <option value="option1">Choose an action...</option> <option value="edit">Edit</option> <option value="delete">Delete</option> </select> <a class="button" href="#">Apply to selected</a> if($result){ echo "<meta http-equiv=\"refresh\" content=\"0;URL=nameslist.php\">"; } } mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/ Share on other sites More sharing options...
SkyRanger Posted May 19, 2011 Author Share Posted May 19, 2011 Anybody able to help with this? Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/#findComment-1217798 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 19, 2011 Share Posted May 19, 2011 While loops are always fun, but for what you wish to achieve, you want to look at another member in the loop family. The 'foreach' loop. This will allow for each individual id in your array to be stored in a single variable. See below example while($rows=mysql_fetch_array($result)){ $theId = $row['id']; $theFname = $row['name']; $theLname = $row['lastname']; $theEmail = $row['email']; $deleteThese .= '<input name="contacts[]" type="checkbox" value="' . $theId . '" /> ' . $theFname . ' ' . $theLname . '<br />' . $theEmail . '<br /><br />'; } Now we want to give a rule for our checkbox function. We want to capture whichever checkbox was ticked, and do something to that particular piece of data in the database. We know which piece of data in the database we will be working with because of our variable $theID declared in each input checkbox. See below how we make the foreach function work for us here if(isset($_POST['delete_contacts']) && !empty($_POST['contacts'])) { I always like to run the above if statement to check whether or not our submit button was pressed, and to ensure that there was actually some piece of information checked on our form before we move forward. foreach($_POST['contacts'] as $allchecked) { Now we do a foreach function. This will grab the value of each checked box on our form. Remember, the value of each checkbox is the current ID that we are working with. This is how we will tell our database what and where to delete. $query = "SELECT id FROM your_table_name WHERE id='$allchecked"; $sql = mysql_query($query) or die (mysql_error()); Here I like to do one more check to make sure our database found someone with the same ID as the checkbox value. $existCount = mysql_num_rows($sql); if($existCount > 0) { After our count of the rows, I do the final query to the database, which is find the contact in the database, and delete that person from the database. $lastQuery = "DELETE FROM your_table_name WHERE id='$allchecked'; $sql2 = mysql_query($lastQuery); Overall, the code will be the following: while($rows=mysql_fetch_array($result)){ $theId = $row['id']; $theFname = $row['name']; $theLname = $row['lastname']; $theEmail = $row['email']; $deleteThese .= '<input name="contacts[]" type="checkbox" value="' . $theId . '" /> ' . $theFname . ' ' . $theLname . '<br />' . $theEmail . '<br /><br />'; } if(isset($_POST['delete_contacts']) && !empty($_POST['contacts'])) { foreach($_POST['contacts'] as $allchecked) { $query = "SELECT id FROM your_table_name WHERE id='$allchecked"; $sql = mysql_query($query) or die (mysql_error()); $existCount = mysql_num_rows($sql); if($existCount > 0) { $lastQuery = "DELETE FROM your_table_name WHERE id='$allchecked'; $sql2 = mysql_query($lastQuery); } } } echo 'anything you want here and be sure to exit when you are done.'; exit(); The above is the simple PHP. Wherever you wish to display the list of options that will be deleted, echo this function in the HTML. See below <?php echo "$deleteThese"; ?> This will give you your list of checkboxes and names with emails listed below. Also, don't forget to add your form and button somewhere on the html. <form action="some_page.php" method="post" enctype="multipart/form-data"> <input type="submit" name="delete_contacts" value"Delete Contacts" /> (NOTE: we establish the 'name' of this button here and it has to be the same name in the php where you are checking to see if this button was clicked) </form> And you are done. The code you are using is not necessary as far as I know. The above is all you need to capture the data from your database and remove it if its been checked. Hope this works, good luck!! Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/#findComment-1217808 Share on other sites More sharing options...
Bl4ckMaj1k Posted May 19, 2011 Share Posted May 19, 2011 P.S. I just realized you want to tell the code to either edit or delete a group....I would handle this on a different page. Ideally you will want your users to be able to delete from blah.php and edit from somewhereelse.php Regarding editing, what exactly are the users editing? The user data? Some name and email data? Depending on what you are editing, I can assist with a script that will allow you to edit multiple rows of user information in one form. Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/#findComment-1217811 Share on other sites More sharing options...
jcbones Posted May 19, 2011 Share Posted May 19, 2011 Lets see the full script, as I am not sure if all of the PHP tags are in the correct positions. Seems this script is out-of-context. Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/#findComment-1217823 Share on other sites More sharing options...
SkyRanger Posted May 19, 2011 Author Share Posted May 19, 2011 The select box is suppose to be above. This code is outside of the form // Check if delete button active, start this if($delete){ for($i=0;$i<$count;$i++){ $del_id = $checkbox[$i]; $sql = "DELETE FROM $tbl_name WHERE id='$del_id'"; $result = mysql_query($sql); } Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/#findComment-1217830 Share on other sites More sharing options...
jcbones Posted May 19, 2011 Share Posted May 19, 2011 Inside the form, outside the form is not the problem. Since technically, everything in PHP is outside of the HTML scope. As it is separate from HTML, and executed before the browser even thinks of displaying the markup. However, the problem does lye in the fact that I don't see any PHP tags around that bit of code. Which means that code is passed to the browser, and never parsed by PHP. Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/#findComment-1217839 Share on other sites More sharing options...
SkyRanger Posted May 20, 2011 Author Share Posted May 20, 2011 Decided to just use buttons instead, the drop down was being to much of a pain, thanks for you help guys, got the multi delete to finally work, will work on the multi edit later. Quote Link to comment https://forums.phpfreaks.com/topic/236812-execute-by-dropdown/#findComment-1217862 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.