Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <select name="cc">
- <?php display_countries(""); ?>
- </select>
- // Call `display_countries` with empty string to all countries
- // Call `display_countries($country_id)` then option will displayed as selected.
- <?php
- function display_countries($country_id) {
- // write your query
- $query = "SELECT country_id, country_name FROM Countries";
- // now run your query with connection or link
- $run_query = $link->query($query);
- // here option will be display and we will stored and get it from the array
- while ($r = $run_query->fetch_array(MYSQL_BOTH)) {
- $is_selected = ""; // first is_selected to empty
- if ($r['country_id'] == $country_id) {
- $is_selected = "selected"; // $country_id is equal to id from the database then its a selected option
- }
- // print the required option for selected with $is_selected variable
- // $r['country_id'] and $r['country_name'] are the column name from database
- echo "<option value='" . $r['country_id'] . " $is_selected '>" . $r['country_name'] . "</option>";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement