Hey guys, so, I'm creating a control panel for my site where I can edit user's priveledges etc. What I'm trying to do is populate a drop down list of usernames in the database to select from.

This is what I've got so far (it's one continuous chunk so I spaced it out for easier reading):

<?php

define('DB_NAME', 'removed');
define('DB_USER', 'removed');
define('DB_PASSWORD', 'removed');
define('DB_HOST', 'localhost');

?>

<select name="username" id="username">
    <option value="">--</option>
    <option value="
    <?php
    $database_link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $db = mysql_select_db(DB_NAME, $database_link);
    $loop = mysql_query("SELECT * FROM registration");
    while($row = mysql_fetch_array($loop)) { echo($row['username']); }
    ?>">

    <?php
    $database_link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $db = mysql_select_db(DB_NAME, $database_link);
    $loop = mysql_query("SELECT * FROM registration");
    while($row = mysql_fetch_array($loop)) { echo($row['username']); }
    ?>
    </option>
</select>

At the moment it returns the usernames but only in a single option box with all the names bunched together.

Thanks :)

Recommended Answers

All 6 Replies

You need to move the option inside the while loop, because every option needs that tag.

What? I use this script to autopopulate a date entry box, it works perfectly:

<?php for ($i = 2014; $i >= 1901; $i--) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>

Parker, you arent using that script

<option is inside the for loop in your second post
<option is outside the while loop in your first post

Pritaeas: 12000 posts,moderator,featured,sponsor,1915 solved, 1544 reputation, 172 skill;
10 on the 0-10 sliding scale of guru

is telling

Parker 1: 20 posts, nul, nul, nul, 0, 0, 0, 0;
the personification of the other end of the 0-10 scale

the answer

Did you at least try it

Now that I reread what I said, I realize what I said, it seems rude. I apologize for that, I wrote it in a rush before work..

Though, I don't see why you thought it was necessary to compare someone's post count on Daniweb. The fact that I found the site two weeks ago doesn't equate to not having any scripting knowledge.

The reason I said that was that I didn't understand at the time what he was saying. Now because of what you added, I realize the difference in my two scripts. I'll try fixing it and see if it works when I get home.

Thanks!

commented: Cant Ask for better than that, good answer, I have no rebuttal +13

something like

<select name="username" id="username">
 <option value="">--</option>
 <?php
  $database_link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  $db = mysql_select_db(DB_NAME, $database_link);
  $loop = mysql_query("SELECT * FROM registration");
  while($row = mysql_fetch_array($loop)) { 
   echo "<option value='$row['username']>$row['username']</option>"; 
  } ?>
</select>

Thanks to you guys I was able to fix it and it works flawlessly!

Thanks guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.