tags:

views:

27

answers:

2

the below code shows a table with users to be accepted or declined. the code as it is has a dropdown on the end allowing to either accept it, deny it or leave as is. there is a button on the bottom to submit the form and after that there should be a php script that decides which user is accepted, denied or still pending.

what would be the best approach to make this work since i find it hard to link the dropdown box and the id.

<table align='center' width='90%'>
                        <tr>
                            <center><td><strong>ID:</strong></td></center>
                            <center><td><strong>Name:</strong></td></center>
                            <center><td><strong>Level:</strong></td></center>
                            <center><td><strong>Job:</strong></td></center>
                            <center><td><strong>Guild:</strong></td></center>
                            <center><td><strong>Date:</strong></td></center>
                            <center><td><strong>Action:</strong></td></center>
                        </tr>
                        <?php
                        $id = 0;
                        $result=mysql_query("SELECT * FROM accounts WHERE active ='0' ORDER BY date LIMIT 10") or die(mysql_error());
                        while($row = mysql_fetch_array( $result )) {
                        $id = $id + 1;
                        ?>
                        <form method='POST' action=''>
                        <tr>
                            <center><td><?php echo $row['id']; ?></td></center>
                            <center><td><?php echo $row['user']; ?></td></center>
                            <center><td><?php echo $row['level']; ?></td></center>
                            <center><td><?php echo $row['job']; ?></td></center>
                            <center><td><?php echo $row['guild']; ?></td></center>
                            <center><td><?php echo $row['date']; ?></td></center>
                            <td>
                                <select name='action_<?php echo $row['id']; ?>'>
                                    <option value='none'>None</option>
                                    <option value='accept'>Accept</option>
                                    <option value='decline'>Decline</option>
                                </select>
                            </td>
                        </tr>
                        <?php } ?>
                        <tr>
                            <br /><td align='right'><input type='submit' value='Submit' name='submit' /></td>
                        </tr>
                        </form>

                    </table>
<?php 
            if(isset($_POST['submit'])) {
                if(isset($_POST['action_'.$row['id'].'']) && $_POST['action_'.$row['id'].''] == "accept" ) {
                    $acc = mysql_query("UPDATE `accounts` SET `active`='1' WHERE `id` = '".$row['id']."'");
                    echo "<meta http-equiv=refresh content=\"5; url=?wesofly=main&page=recruitment\">";
                }elseif(isset($_POST['action_'.$row['id'].'']) && $_POST['action_'.$row['id'].''] == "decline" ) {
                    $dec = mysql_query("DELETE * from `accounts` WHERE '".$row['id']."'");
                    echo "<meta http-equiv=refresh content=\"0; url=?wesofly=main&page=recruitment\">";
                }
            }
?>
+1  A: 

I think you'll be better off with HTML form arrays, for instance:

<select name="action[<?php echo $row['id']; ?>]">
    <option value="none">None</option>
    <option value="accept">Accept</option>
    <option value="decline">Decline</option>
</select>

And inside your php code, you would simply loop through the submitted action field in your $_POST array to find which id had which action. Each entry would be presented as an array, the key would be the user's id.

kovshenin
+1  A: 

Your code is confusing. Your WHILE loop starts before the form tag, but the WHILE loop closes before the form tag closes. This means that you are going to have multiple form elements.

So, first of all, change that in your code. Next the way you want this to work is using arrays.

Your drop down box should look like this

<select name='action[<?php echo $row['id']; ?>]'>
  <option value='none'>None</option>
  <option value='accept'>Accept</option>
  <option value='decline'>Decline</option>
</select>

Then on the PHP side, you should be able to do something like

foreach ($_REQUEST['action'] as $key => $value) {
   $id = $key;
   $status = $value;
   // put your code here to update the specific database item
} 
Codemwnci
We think alike ;)
kovshenin
indeed we do! And looks like you beat me to the answer by a few seconds. At least having two similar answers helps confirm the answer is a good one :o)
Codemwnci
thanks for the answers. how would you go and fetch the array values? not good with arrays in php
Andy
thanks, you missed out an ; though.
Andy