oliverj777 Posted August 27, 2010 Share Posted August 27, 2010 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! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2010 Share Posted August 27, 2010 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>"; Quote Link to comment Share on other sites More sharing options...
oliverj777 Posted August 27, 2010 Author Share Posted August 27, 2010 I'm getting a syntax error on: while ($array = mysql_fetch_assoc($result)) { Quote Link to comment Share on other sites More sharing options...
oliverj777 Posted August 27, 2010 Author Share Posted August 27, 2010 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? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2010 Share Posted August 27, 2010 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() ); Quote Link to comment Share on other sites More sharing options...
oliverj777 Posted August 27, 2010 Author Share Posted August 27, 2010 I want the username to show up and also be the value ... how do I go about doing that? Quote Link to comment Share on other sites More sharing options...
oliverj777 Posted August 27, 2010 Author Share Posted August 27, 2010 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 .... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 27, 2010 Share Posted August 27, 2010 It may mean the query failed. Did you actually echo the query to make sure it's correct? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.