One good day I was working for building administration part of a complex Web Site. And like in other administration systems it must have the opportunity to view all users and apply some actions upon them. In my case I had a list of users and a checkbox control near them, and I had to make a delete action for selected users.
How did I find solution? Here is some piece of code.
Print list of users:
echo ‘<form action=”‘.$_SERVER['REQUEST_URI'].‘” method=”post”>’;
echo ‘<table>’;
while ( )
{
echo ‘<tr>’;
echo ‘<td><input type=”checkbox” name=”selected_user['.$user['id'].‘]” value=”1″ ></td>’;
echo ‘<td>’.$user['username'].‘</td>’;
…
echo ‘</tr>’;
}
echo ‘</table>’;
echo ‘<input type=”submit” name=”delete_user” value=”Delete”>’;
echo ‘</form>’;
When submit button “Delete” was activated:
if ($_POST["delete_user"])
{
foreach($_POST["selected_user"] as $id=>$value)
{
// delete user with userid == $id
}
}
So, at last the foreach statement gives to me “id” of selected user, and I can apply the action upon this user. It was interesting for me to find this solution of proceeding variables in PHP.