jjmusicpro Posted September 26, 2007 Share Posted September 26, 2007 i wanted to query my db, and then add values to drop down menu, where when they select what they want in the dropdown, it will bring them to another page accounts.php where they will have more options, any help? Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/ Share on other sites More sharing options...
BlueSkyIS Posted September 26, 2007 Share Posted September 26, 2007 1. Query the database to get the ids and/or values 2. Open a SELECT control: <SELECT name='something'> 3. Loop over the query results, inserting an OPTION for each one: <OPTION value='{$row['id']}'>{$row['name']}</OPTION> or similar... 4. Close the select: </SELECT> 5. On POST, check the value of the control: $someval = $_POST['something']; 6. header to whatever page you want to go to: header("location: someotherpage.php");exit; Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355760 Share on other sites More sharing options...
jjmusicpro Posted September 26, 2007 Author Share Posted September 26, 2007 Do you have a code example? I am new to php, thank you for all your help :D Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355779 Share on other sites More sharing options...
hamza Posted September 26, 2007 Share Posted September 26, 2007 Adding in any menu is quite simple. ............................................................... Just select the information or data from the database. Use the <select> tag and put the selected information into the <option> tag . And your menu will be created. its quite simple just try it . Reply me if you face any problem. ??? ............................................................. Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355857 Share on other sites More sharing options...
jjmusicpro Posted September 26, 2007 Author Share Posted September 26, 2007 I did try it, and gives error Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355929 Share on other sites More sharing options...
mattal999 Posted September 26, 2007 Share Posted September 26, 2007 <form action='accounts.php' method='GET'> <select name='name'> <?php parse_str("$QUERY_STRING"); $db = mysql_connect("[your host - usually "localhost"]", "[your username]","[your password]") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("[your database]",$db)) die("No database selected."); $acc1="SELECT * from accounts"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_array($acc2)) { echo "<option value='$query[name]'>$query[name]</option>"; } ?> </select> <input type='submit' name='submit' value='Submit'> </form> that would connect to a mysql database, select table accounts with one field, name, and print all the rows. Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355934 Share on other sites More sharing options...
jjmusicpro Posted September 26, 2007 Author Share Posted September 26, 2007 thank you very much, now i have a column named owner, and i have a owner listed multiple times, how do i make it only display unique results? Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355937 Share on other sites More sharing options...
jjmusicpro Posted September 26, 2007 Author Share Posted September 26, 2007 I tried to run that code and it didnt work It builds the page, and the drop down, but nothings in the drop down I have a table called zipcodes, with a column called account_name <form action='nothing.php' method='GET'> <select name='name'> <?php parse_str("$QUERY_STRING"); $db = mysql_connect("[localhost]", "[xxxxxxx]","[xxxxx]") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("[xxxxxx]",$db)) die("No database selected."); $acc1="SELECT * from zipcodes"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_array($acc2)) { echo "<option value='$query[account_name]'>$query[account_name]</option>"; } ?> </select> <input type='submit' name='submit' value='Submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355945 Share on other sites More sharing options...
jjmusicpro Posted September 26, 2007 Author Share Posted September 26, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355969 Share on other sites More sharing options...
marcus Posted September 26, 2007 Share Posted September 26, 2007 $sql = "SELECT * FROM `table`"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ echo "No values in database!\n"; }else { echo "<select name=\"zip\">\n"; while($row = mysql_fetch_assoc($res)){ echo "<option value=\"$row[zip]\">$row[town]</option>\n"; } echo "</select>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355972 Share on other sites More sharing options...
jjmusicpro Posted September 26, 2007 Author Share Posted September 26, 2007 I tried that but didnt work: Still gives blank drop down... <form action='nothing.php' method='GET'> <select name='name'> <?php parse_str("$QUERY_STRING"); $db = mysql_connect("[localhost]", "[xxxxx]","[xxxxxxx]") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("[xxxxxxx]",$db)) die("No database selected."); $sql = "SELECT * FROM `zipcodes`"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ echo "No values in database!\n"; }else { echo "<select name=\"zip\">\n"; while($row = mysql_fetch_assoc($res)){ echo "<option value=\"$row[account_name]\">$row[iD]</option>\n"; } echo "</select>\n"; } ?> </select> <input type='submit' name='submit' value='Submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355975 Share on other sites More sharing options...
marcus Posted September 26, 2007 Share Posted September 26, 2007 Is $row[iD] in caps? Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-355976 Share on other sites More sharing options...
jjmusicpro Posted September 27, 2007 Author Share Posted September 27, 2007 I changed it but sill didnt want to work: The page comes up with a dropdown, but nothing is in the drop down: <form action='nothing.php' method='GET'>Select Account:<br> <select name='name'> <?php parse_str("$QUERY_STRING"); $db = mysql_connect("[localhost]", "[xxxxxx]","[xxxxx]") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("xxxxxxx]",$db)) die("No database selected."); $sql = "SELECT * FROM `zipcodes`"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ echo "No values in database!\n"; }else { echo "<select name=\"zip\">\n"; while($row = mysql_fetch_assoc($res)){ echo "<option value=\"$row[account_name]\">$row[search_id]</option>\n"; } echo "</select>\n"; } ?> </select> <input type='submit' name='submit' value='Submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-356505 Share on other sites More sharing options...
mattal999 Posted September 27, 2007 Share Posted September 27, 2007 <form action='nothing.php' method='GET'> <select name='name'> <?php parse_str("$QUERY_STRING"); $db = mysql_connect("localhost", "username","password") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("dbname",$db)) die("No database selected."); $acc1="SELECT * from zipcodes"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_array($acc2)) { echo "<option value='$acc3[account_name]'>$acc3[search_id]</option>"; // was $query[account_name] - not set, i set $acc3 - this was my old one } ?> </select> <input type='submit' name='submit' value='Submit'> </form> Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-356522 Share on other sites More sharing options...
jjmusicpro Posted September 27, 2007 Author Share Posted September 27, 2007 Thank you very much, how do i limit the results to just 1 instance of that account_name found? Right now, i might have 4 or 5 account_name listed the same, and it will put all 5 of those in the drop down, i just want 1 of each of the uniquie account_name it finds Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-356530 Share on other sites More sharing options...
jjmusicpro Posted September 27, 2007 Author Share Posted September 27, 2007 I got it i just added this: $acc1="SELECT account_name from zipcodes group by account_name"; Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/70761-solved-query-from-db-add-values-in-dropdown-menu/#findComment-356532 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.