O'Reilly Forums: Data-driven Dropdown - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Data-driven Dropdown

#1 User is offline   thoward 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 19-February 14

Posted 22 August 2014 - 06:00 AM

This isn't a question directly from the book. It's more of a side-project that I have been working on, inspired by the data-driven forms section on pages 450-457. 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 connection
mysqli_close($dbc);
?>

</select>
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users