Ok, so let’s say I have a list of users in a database. I want to get that list out on the screen and allow someone to be able to use checkboxes to do something with each of those users. In this example we’re going to say we are deleting any users that have been checked. If you are not already familiar with building dynamic output from mysql then I suggest you first read over Web Dev> Populate PHP/HTML table from MySQL database
First we’ll build our form to show our users, in this example I’m actually building my list of users from a query to a mail server which gets returned to me first as a string. I need to change this into an array so I’m going to use explode(). I’ll go through this all step by step so it’s easy to follow along and you can skip whatever parts you feel you are already familiar with.
So lets say the mail server returns this string to me of users:
$members_string = 'Bruce Chuck Jackie Jet'; echo "Members string is '$members_string'";
Which gives us an output of
Members string is 'Bruce Chuck Jackie Jet'
Since I want to loop through each of these members I need to first create an array that I can loop through, this is where explode() comes in.
// Members are separated by spaces, so explode on spaces $members = explode(' ',$members_string);
Now we should have an array of members. Let’s check our work and make sure we are getting what we expected.
print_r($members);
Which should give us an output of
Array ( [0] => Bruce [1] => Chuck [2] => Jackie [3] => Jet )
Perfect! Our $members array is now ready for looping. I am going to use a foreach() to go through each element of the array:
// Before we we enter the loop lets begin the table echo "<table>"; // And we'll give it a header row so we know what each of these columns represents echo "<tr><td>Delete</td><td>Member</td></tr>"; <?php $members_string = 'Bruce Chuck Jackie Jet'; $members = explode(' ',$members_string); $num_subs = count($members); foreach($members as $member){ echo "<tr><td><input type='checkbox' value='".$member."' name='deletemember[]' /><input type='hidden' value='".$num_subs."' name='count' /></td><td>$member</td></tr>"; } ?> // We're out of the loop, let's add a submit button and close our table <tr><td colspan=2><input type='submit' value='Delete Checked' style='float: right;' name='deletemember_submit' /></td></tr> echo "</table>";
Alright so now we have our table that should look something like this (my webpage automatically styled it because it’s a table — yours will be a little uglier:
Delete | Member | $member |
Everything should look good now. Let me explain a few things in what we just typed up here.
$member, this is the single entry for a member from the $members array (So in the first iteration the member is Bruce)
deletemember[], this is the name of our checkbox, we are using the [] so that when we post our checkbox named deletemember will be pushed into an array we can loop through for processing each individual.
$num_subs, this is a count the number of elements in our $members array — in this particular example that number should be 4 because we know we have 4 members.
Now that we have everything setup on the form side we need to worry about what happens once the form is submitted.
Back up somewhere near the top of our file (so that it runs before the form is generated) we want to add the following code:
if(isset($_POST['deletemember_submit'])){ $count = $_POST['count']; if(isset($_POST['deletemember'])){ $deletemember = $_POST['deletemember']; for($i=0;$i<$count;$i++){ if(isset($deletemember[$i])){ echo "Deleting $deletemember[$i]"; // Code to delete user } } } }
Let me explain what everything here does line by line…
- Checks to see if the form that controls deletions (the one we just made) has been submitted, if it has then it proceeds to Line 2
- Sets the $count variable to what we had as our hidden variable $num_subs, which we assigned the name of ‘count’
- Checks to see if any members have actually been marked for deletion (users could submit the form without anyone actually checked and it would throw a Notice if we didn’t check for this
- Sets the $deletemember array to the array we created in $_POST[‘deletemember’] (via deletemember[])
- This is our for loop, which has 3 parts. $i starts at 0, then with each loop it tells it to continue while $i is less than $count (remember $i starts at 0, so the first time it runs $i will be 0 and $count will be 4, so by the time it ends $i will be 3 and $count 4), the last part says to increase $i once that looping has completed.
- If the deletemember[$i] has actually been set to “on” (checked) then continue
- Just an echo to check we are deleting the correct member
- Whatever code you need to write to actually delete the member from wherever you retrieved it from. You’ll probably also want to put in here something to check that your deletion from whatever system was successful or failed and report the outcome somewhere.
That’s it!
This code is very close to what I need, but I’m attempting to select one and only one record and then use the selection to populate a report on a web page. When I attempt to run your code I get an error “Notice: Undefined variable: num_subs in C:\xampp183\htdocs\cforce\testing.php on line 27”. What am I missing?
Thanks.
Jim
James, I found that some of the code was missing under the “Perfect!” section. Please copy and paste that section again and you should now have it defined.
Scott, I added form tags and got this to work.
Jim
Scott, thanks for getting back to me, but in spite of my best efforts I cannot get this code to work. For instance when I run the code, I get the page with the table, but when I select a name for deletion and click the “Delete Checked” button I do not get the echoed “Deleting (deletemember)”. This indicates to me that the block of code, specifically line 7, detecting the selection is not being executed. Did I miss something?
Jim