Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <select name="cc">
  2.     <?php display_countries(""); ?>
  3. </select>
  4.  
  5. // Call `display_countries` with empty string to all countries
  6. // Call `display_countries($country_id)` then option will displayed as selected.
  7.  
  8.  
  9. <?php
  10.  
  11. function display_countries($country_id) {
  12.  
  13.     // write your query
  14.     $query = "SELECT country_id, country_name FROM Countries";
  15.    
  16.     // now run your query with connection or link
  17.     $run_query = $link->query($query);
  18.  
  19.     // here option will be display and we will stored and get it from the array
  20.     while ($r = $run_query->fetch_array(MYSQL_BOTH)) {
  21.  
  22.         $is_selected = "";  // first is_selected to empty
  23.         if ($r['country_id'] == $country_id) {
  24.             $is_selected = "selected"; // $country_id is equal to id from the database then its a selected option
  25.         }
  26.        
  27.         // print the required option for selected with $is_selected variable
  28.         // $r['country_id'] and $r['country_name'] are the column name from database
  29.         echo "<option value='" . $r['country_id'] . " $is_selected '>" . $r['country_name'] . "</option>";
  30.        
  31.  
  32.     }
  33.  
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement