Jump to content

Populating a drop down box from values in databases - HELP


oliverj777

Recommended Posts

Basically,

 

I have a database table called 'users' and I would like to populate a drop down box with these values of 'users'.

 

How?? - to call upon the values is this: 'upduser2'

 

Right now, all I am using is a text box, in where you have to type in the users name manually (this is so an admin can change variables and settings according to that current user). This is what I am using so far:

 

 

<h3>Update User Level</h3>
                                                    <? echo $form->error("upduser"); ?>
                                                    <table>
                                                      <form action="adminprocess.php" method="post">
                                                        <tr>
                                                          <td> Username:<br />
                    <input type="text" name="upduser" maxlength="30" value="<? echo $form->value("upduser"); ?>" /></td>
                                                          <td> Level:<br />
                                                          <select name="updlevel">
                                                              <option value="1">1 </option> //example settings
                                                              <option value="9">9 </option>
                                                            </select></td>
                                                          <td><br />
                                                            <input type="hidden" name="subupdlevel" value="1" />
                                                            <input type="submit" value="Update Level" /></td>
                                                        </tr>
                                                      </form>
                                                    </table></td>
                                                </tr>

 

 

Much help would be appreciated.

Say your database is called "USERS" and you have fields like "username", you can populate the dropdown list with the values of "username" as follows:

<?php
$result = mysqli_query($link, 'SELECT * FROM users');        
while ($row = mysqli_fetch_array($result))  { $users[] = array('username' => $row['username']); } 
?>
<form>
<select name="users">
<?php foreach ($users as $user):  ?>
<option value="<?php echo $user['username']; ?>"> <?php echo $user['username']; ?></option>
<?php endforeach; ?>
</select>
<form>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.