Andrew R Posted August 3, 2006 Share Posted August 3, 2006 HelloI was wondering how I could display all the users from my database in the drop down list so when I submit the form (viewusers.php) it displays what user I selected from the drop down form in viewusers.php.At the top of the page I have all the database information and the sql query.[quote]<table width="925" border="0" cellpadding="0" cellspacing="0"> <!--DWLayoutTable--> <tr> <td width="104" height="24" valign="middle">User:</td> <th width="221" valign="middle" scope="col"> <form action="viewusers.php" method="post" name="user" id="user"> <div align="left"> <select name="formquery" class="table" id="formquery"> </select> <input name="formtype" type="hidden" id="formtype" value="user"> <input name="Submit" type="submit" class="table" value="Go!"> </div> </form></th> <th width="592"> </th> </tr></table>[/quote]Help would be much appreciated.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/ Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 I hope this helps[code=php:0]$query = "SELECT * FROM users WHERE username LIKE '%".$usersearch."'";$result = mysql_query($query);if(mysql_num_rows($result) == 0){echo "I am afraid no users were found";}else{echo "<form method='post' action='page.php'>\n"."<select name='users'>\n";while($row = mysql_fetch_assoc($result)){echo "<option>".$row["username"]."</option>\n";}echo "</select>\n"."</form>\n";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68491 Share on other sites More sharing options...
Andrew R Posted August 3, 2006 Author Share Posted August 3, 2006 Cheers for the help onlyican although I was trying to do it this way (Below). The problem I am having using the below script is that the drop down form only displays one username, not all the users in the database. Any ideas on how to fix it?[code]<?phpinclude 'db.php';$query_users = "SELECT * FROM users";$users = mysql_query($query_users) or die(mysql_error());$row_users = mysql_fetch_assoc($users);php ?><table width="925" border="0" cellpadding="0" cellspacing="0"> <!--DWLayoutTable--> <tr> <td width="104" height="24" valign="middle">Day:</td> <td width="438" valign="top"><form action="viewusers.php" method="post" name="user" id="user"> <div align="left"> <input name="formtype" type="hidden" id="formtype" value="users"> <select name="formquery" id="formquery"> <option value="<?php echo $row_users['username']?>"><?php echo $row_users['username']?></option> </select> <input name="Submit" type="submit" class="table" value="Go!"> </div> </form></td> <td width="383"> </td> </tr></table>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68530 Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 you need to loop it in a while loop<select name="formquery" id="formquery"><?while ($row = mysql_fetch_assoc($users){echo "<option>".$row["users"]."</option>\n";}?></select>Note: if the value of the option is the same as whats between the two options, you dont need to add a value. It will still work Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68532 Share on other sites More sharing options...
CTM Posted August 3, 2006 Share Posted August 3, 2006 That's because you weren't looping through all the results, simply using the first one. Now, onlyican's solution works, but you could always do something like :[code=php:0]<?phpinclude 'db.php' ; // Including the DB configuration, connection and such I presume$query_users = "SELECT username FROM users" ; // Instead of *, since you're only using the username field, only fetch that (for memory issues)$users = mysql_query ( $query_users ) or die ( mysql_error ( ) ) ;$select_options = array ( ) ; // Instead of using a string, we'll use an array into which we'll store every result (in the <option> tag form)while ( $row_user = mysql_fetch_assoc ( $users ) ) // Loop it up{ // The [] after select_options indicates to append a new value to the select_options array $select_options[] = "<option value = '" . $row_user['username'] . "'>" . $row_user['username'] . "</option>" ;}$select_options = implode ( "<br />", $select_options ) ; // I'm simply using this (and the array form) for formatting preferences (when viewing the HTML source)// Now, you have a long string which contains every option that goes inside your select element.?><table width="925" border="0" cellpadding="0" cellspacing="0"> <!--DWLayoutTable--> <tr> <td width="104" height="24" valign="middle">Day:</td> <td width="438" valign="top"><form action="viewusers.php" method="post" name="user" id="user"> <div align="left"> <input name="formtype" type="hidden" id="formtype" value="users"> <select name="formquery" id="formquery"> <?php print $select_options ; ?> </select> <input name="Submit" type="submit" class="table" value="Go!"> </div> </form></td> <td width="383"> </td> </tr></table>[/code]Anyway, onlyican's should work fine too, but it can't be re-used. Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68534 Share on other sites More sharing options...
Andrew R Posted August 3, 2006 Author Share Posted August 3, 2006 Cheers guys, I tried both scripts, both worked well although I also want to add another drop down box containing the location of all of the members, the problem is USA and UK are being displayed over and over again in the drop down box because they are where most our members are from, how would I stop that so the USA and the UK will only display once in the drop down?Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68574 Share on other sites More sharing options...
onlyican Posted August 3, 2006 Share Posted August 3, 2006 use another query, and add GROUP BY country Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68576 Share on other sites More sharing options...
CTM Posted August 3, 2006 Share Posted August 3, 2006 You wouldn't really need a second query if using my method, but then again, a second query would speed the process of treating the country info. I'm not sure in memory usage which one would be faster, but the second query is definitively simpler. Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68736 Share on other sites More sharing options...
Andrew R Posted August 3, 2006 Author Share Posted August 3, 2006 Thanks guys for your help. To add a second select_options array would I simply add [quote]$select_options[2] = "<option value = '" . $row_user['username'] . "'>" . $row_user['username'] . "</option>" ;[/quote] below the first select array?Cheers Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68810 Share on other sites More sharing options...
CTM Posted August 3, 2006 Share Posted August 3, 2006 No. And I'm not sure what you want. You'd want an exact replica of the dropdown options above? Or another one with different info?As I explained my comments, the $select_options[] lines is simply to indicate that you're appending a new value into the $select_options array (which, in that instance, was the usual "<option value='username'>username</option>"). $select_options[2] will simply return the third iteiration of that array, which means a singleton of type "<option value='username'>username</option>".Anyway, explain what you want. Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68816 Share on other sites More sharing options...
redarrow Posted August 3, 2006 Share Posted August 3, 2006 Try this please.[code]<?phpinclude 'db.php';$query_users = "SELECT * FROM users";$users = mysql_query($query_users) or die(mysql_error());while($row_users = mysql_fetch_assoc($users)) {php ?><table width="925" border="0" cellpadding="0" cellspacing="0"> <!--DWLayoutTable--> <tr> <td width="104" height="24" valign="middle">Day:</td> <td width="438" valign="top"><form action="viewusers.php" method="post" name="user" id="user"> <div align="left"> <input name="formtype" type="hidden" id="formtype" value="users"> <select name="formquery" id="formquery"> <option value="<?php echo $row_users['username']?>"><?php echo $row_users['username']?></option> </select> <input name="Submit" type="submit" class="table" value="Go!"> </div> </form></td> <td width="383"> </td> </tr></table><?}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/16443-displaying-information-from-a-database-in-a-drop-down-form/#findComment-68818 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.