Jump to content

Help With Dynamic Drop Box - HELP!!!


oliverj777

Recommended Posts

I really can't get my head around this dynamic drop box. What I want is for a drop box to be populated with values in my database.

 

I have this that connects to my SQL and picks out the table required:

 

function displayUsers(){
   global $database;
   $q = "SELECT username,"
       ."FROM ".TBL_USERS." ORDER BY userlevel DESC,username";
   $result = $database->query($q);    ...

 

I then have this to pick out any errors, and also using the num_rows to get the number of rows (values) there are:

 

$num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }

 

From here, I guess I want the num_rows to keep 'adding on' the number of <option value=""> in my selection box according the number of values I have in my database.

 

At this point, I can pull out the values into a dynamic table ... but I want it into a drop box -- but I'll but up the code for the dynamic table so you can get an idea:

 

 

/* Display table contents */
   echo "<table align=\"left\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n";
   echo "<tr><td><b>Username</b></td></tr>\n";
   for($i=0; $i<$num_rows; $i++){
      $uname  = mysql_result($result,$i,"username");
      echo "<tr><td>$uname</td></tr>\n";
   }
   echo "</table><br>\n";
}

 

I hope you can use the code above to help me develop this darn drop box!

 

Thanks, Ollie!

Link to comment
Share on other sites

I usually approach this a little differently. I'd select the username along with the PK id that's associated with with the record and use those for the <select>.

 

$query = "SELECT `username`, `id` FROM `table` ORDER BY `whatever`";
$result= mysql_query($query);
echo "<select name=\"my_select_name\">
while( $array = mysql_fetch_assoc($result) ) {
     echo "<option value=\"{$array['id']}\">{$array['username']}</option>";
}
echo "</select>";

Link to comment
Share on other sites

Okay, So I've done it like this:

 

$query = "SELECT username"."FROM ".TBL_USERS;
$result= mysql_query($query);
echo "<select name=\"my_select_name\">";
while ($array = mysql_fetch_assoc($result)) {
     echo "<option value=\"{$array['id']}\">{$array['username']}</option>";
}
echo "</select>";

 

But nothing shows up in the box - its empty.

 

What now?

Link to comment
Share on other sites

You're selecting only the 'username' filed from the table, but trying to echo both the 'username' and 'id' fields.

Echo your query to the screen, and make sure it contains the correct values. You can then also paste it into phpMyAdmin to see what it returns.

Add an 'or die()' to the query execution for debugging. $result = mysql_query($query) or die( mysql_error() );

Link to comment
Share on other sites

Okay - So now I've done this -->

 

 

  global $database;
  $q = "SELECT username"
       ."FROM ".TBL_USERS." ORDER BY username";
$result = $database->query($q);
echo "<select name=\"my_select_name\">";
while ($array = mysql_fetch_assoc($result)) {
     echo "<option value=\"{$array['id']}\">{$array['username']}</option>";
}
echo "</select>";
  
$result = mysql_query($query) or die( mysql_error() );

 

Nothing still is being displayed - and the $result = 'Query was empty' - which I guess means that there is nothing in that field? (because there is!)

 

Help ....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.