thoward Posted August 27, 2014 Share Posted August 27, 2014 I currently have an HTML form where the options for a certain drop-down menu are hard-coded. Instead, I want to use PHP to...1. Look up the values in a column (cities) in a MySQL table (locations)2. Make those values the only options in the dropdown menu.Any ideas how I would do this? This is what I have so far.<label for="city">What is your destination city?</label><select class="form-control" id="city" name="city"><?php//connect to the database$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);//grab the city names from the MySQL table $query = "SELECT cities FROM locations";$res = mysqli_query($dbc, $query);while ($data = mysqli_fetch_assoc($res)) {echo '<option value="'.$data['cities'].'">'.$data['cities'].'</option>';}//close the db connectionmysqli_close($dbc);?></select> Quote Link to comment https://forums.phpfreaks.com/topic/290680-data-driven-dropdown/ Share on other sites More sharing options...
cyberRobot Posted August 27, 2014 Share Posted August 27, 2014 That looks like a viable solution so far. Have you tried the code? If so, does it not work...are you getting errors? Quote Link to comment https://forums.phpfreaks.com/topic/290680-data-driven-dropdown/#findComment-1489054 Share on other sites More sharing options...
thoward Posted August 27, 2014 Author Share Posted August 27, 2014 I've tried it out. The dropdown menu appears, but the fields are empty. Quote Link to comment https://forums.phpfreaks.com/topic/290680-data-driven-dropdown/#findComment-1489057 Share on other sites More sharing options...
Ch0cu3r Posted August 27, 2014 Share Posted August 27, 2014 Your code looks fine and should be generating the <option></option> tags for each city in your locations table. If no options are being generate you need to start debugging your PHP code to see where it failing. I'd start by checking mysql has not returned an error. Use mysqli_error to check. Quote Link to comment https://forums.phpfreaks.com/topic/290680-data-driven-dropdown/#findComment-1489062 Share on other sites More sharing options...
Psycho Posted August 27, 2014 Share Posted August 27, 2014 (edited) I agree with Ch0cu3r, the DB connection or query are likely failing - but you aren't checking for errors. I'd also suggest separating the "Logic" from the "Output". Makes debugging much easier. Try this. <?php //connect to the database $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (mysqli_connect_errno()) { die("Connect failed: " . mysqli_connect_error()); } //grab the city names from the MySQL table $query = "SELECT cities FROM locations"; $result = mysqli_query($dbc, $query); if(!$result) { die("Query failed: " . mysqli_error($dbc)): } $cityOptions = ''; while ($row = mysqli_fetch_assoc($result)) { $cityOptions .= "<option value='{$row['cities']}'>{$data['cities']}</option>\n"; } //close the db connection mysqli_close($dbc); ?> Select a city: <select name="city"> <?php echo $cityOptions; ?> </select> If you still get a blank select with no errors, then I'd guess that there are no values for 'cities' in the 'locations' table. But, you could check for that as well. Edited August 27, 2014 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/290680-data-driven-dropdown/#findComment-1489066 Share on other sites More sharing options...
Solution thoward Posted August 28, 2014 Author Solution Share Posted August 28, 2014 Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/290680-data-driven-dropdown/#findComment-1489149 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.