0

I have a Widget which I am creating but my quesiton is how do I access my custom table which I created via my plugin. I am attempting to fill the dropdown from the table called tfp which contains the column liters which I wish to popluate the values into the drop down

class my_plugin extends WP_Widget {

// constructor
function my_plugin() {
        parent::WP_Widget(false, $name = __('Thomsons Fuels Widget', 'wp_widget_plugin') );

}

// widget form creation
function form($instance) {

//Check values
if( $instance) {
 $title = esc_attr($instance['title']);
 $dropdown= esc_attr($instance['dropdown']);
 $textarea = $instance['textarea'];
 } else {
 $title = '';
 $textarea = '';
}
?>


<p>
<label for="<?php echo $this->get_field_id('dropdown'); ?>">
<?php _e('Liters',   'wp_widget_plugin'); ?></label>
<select>
<option value="volvo">Volvo</option>
</select>

</p>

How I created the table in the plugin

global $wpdb;
global $custom_table_example_db_version;

$table_name = $wpdb->prefix . 'tfp'; // do not forget about tables prefix

// sql to create your table
// NOTICE that:
// 1. each field MUST be in separate line
// 2. There must be two spaces between PRIMARY KEY and its name
//    Like this: PRIMARY KEY[space][space](id)
// otherwise dbDelta will not work
$sql = "CREATE TABLE " . $table_name . " (
  id int(11) NOT NULL AUTO_INCREMENT,
  liters VARCHAR(100) NOT NULL,
  price VARCHAR(100) NOT NULL,   
  postcodearea varchar(20) NULL,      
  dft varchar(20) NULL,
  PRIMARY KEY  (id)
);";

// we do not execute sql directly
// we are calling dbDelta which cant migrate database
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);

// save current database version for later use (on upgrade)
add_option('custom_table_example_db_version', $custom_table_example_db_version);
0

1 Answer 1

1

Read: SELECT_a_Column

$sql = "SELECT 'liters' FROM " . $wpdb->prefix . "tfp";

$liters = $wpdb->get_col( $sql );
if( $liters ) { 
  foreach( $liters as $liter ) { 
        echo '<option value="' . $liter . '">' . $liter . '</option>';
  }
}
1
  • their must be an error in this as liter is showing up in the description element of the drop down box Commented Oct 22, 2014 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.