0

I am creating custom WordPress plugin where I want to fetch data from database and show it in select box. I have provided screen shot of table .

Dropdown should be in this format.

<select class="smsselect">
    <option value="1">Select Country</option>
    <option value="2">Select Pakistan</option>
    .....
    </select>

Table name > WP_country > Screenshot of table

Select Box > Screenshot of select box

Sudharshan Nair
  • 1,152
  • 1
  • 10
  • 24
Mr SALLO
  • 31
  • 5

3 Answers3

0

after you fetch the data from the table

foreach( $countries as $country ) {
  echo '<option value="'. $country->id .'" >  Select ' . $country->countryname . '</option>';

} 
Ash0ur
  • 1,505
  • 2
  • 8
  • 11
0

Please use the code below..

Global $wpdb;
$results = $wpdb->get_results("Select * from wp_country");
if(!empty($results)){
  echo "<select name='id'>";
    foreach($results as $result){
       $id = $result->id;
       $name = $result->name; 
       echo '<option value="'.$id.'">'.$name.'</option>';
    }
  echo "</select>";
}

There is no need to connect to database in WordPress.. $wpdb has full configuration of database, it will connect to database automatically.

Yogesh Garg
  • 299
  • 3
  • 10
0

try & stick with the basics, dont have to re-invent the wheel, wordpress has loads of built in functions for various tasks, lots of these relate to the database.. take a look at the WPDB class

in your plugin admin page call a function to display the option list via..

function display_countryList()
{
   global $wpdb;
   $countries = $wpdb->get_results( "SELECT id, countryname FROM {$wpdb->prefix}country");

   $html = '<select id="countryList">';
   $html .= '<option value="">Select Cuntry</option>'; //  :P
   foreach( $countries as $country ) 
   {
      $html .= '<option value="'.$country->id.'">'.$country->countryname.'</option>';
   } 
   $html .= '</select>';

   echo $html;
}
Marty
  • 4,619
  • 2
  • 24
  • 35