0

(added the code; I thought I had it in here) I am trying to build a widget that has a dropdown.

   public function form($instance) {
       global $wpdb;
       global $event;
       global $events;
       $events = $wpdb->get_results ( "SELECT id, event FROM $wpdb->mo_Event" );         

       name;?>
    <label for="<?php echo $this->get_field_id('event'); ?>">'Select an Event'</label>
      <select class="dropdown" id="mydropdown" name="mydropdown" title="My Dropdown">
      <?php
        foreach ($events as $value) {
            echo '<option value="' . unserialize($value) . '">' . unserialize($value) . '</option>';
      } // for each
    ?>          
  </select>
<?php    
} //  function form   

I have no idea what is going in here. My query is : SELECT id, event FROM $wpdb->mo_Event, but he debug log shows : The debug.log shows: SELECT event FROM [30-Apr-2020 15:35:04 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 for query SELECT event FROM made by wp_list_widgets, wp_widget_control, call_user_func_array, WP_Widget->form_callback, gdd_Events_widget->form

The plugin Activates and widget has a small, empty dropdown.

Can someone help me get this right so that I can make a proper widget?

Thanks...Dan'l

2
  • It's hard to read without formatted code, but it looks like you are trying to combine raw MYSQL with $wpdb, which is not how $wpdb works. You can likely use a built-in WP function instead of MYSQL - if "event" is a custom post type, you could use WP_Query to get them. developer.wordpress.org/reference/classes/wp_query
    – WebElaine
    Apr 30, 2020 at 18:39
  • Thanks. Looking into this. Apr 30, 2020 at 21:59

1 Answer 1

0

I finally got the widget built. I hope this will help another newbie.

<?php

/*
 *  Plugin Name: Events Widget
  Description: This widget allows the user to select an Event name.
  Version: 0.5
  Author: Dan Statham
 */


function gdd_Events_load_widgets() {
    write_log('gdd _Events_load_widgets');
}// gdd_Events_load_widgets

// Register the widget
function my_register_events_widget() {
    register_widget( 'gdd_Events_load_widgets' );
} //  echo('registerd');

function submit(){
   if(isset($_POST['submit'])){
      $selected_val = $_POST['Color'];  // Storing Selected Value In Variable
     echo "You have selected :" .$selected_val;  // Displaying Selected Value
}

}
add_action( 'widgets_init', 'my_register_events_widget' );  


// The widget class
class gdd_Events_load_widgets extends WP_Widget {
    function gdd_Events_Widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'event', 
                    'description' => __('Allow the user to select an Event name.', 'event') );

        /* Widget control settings. */
        $control_ops = array( 'width' => 200, 'height' => 150, 'id_base' => 'gdd_Events_Widget' );

        /* Create the widget. */
        $this->WP_Widget( 'Event', __('Event Name', 'event'), $widget_ops, $control_ops );
    } // function gdd_Events_Widget
    // Main constructor
    function __construct() {
        parent::__construct(                
                // base ID of the widget
                'gdd_Events_Widget',
                // name of the widget
                __('Event Names', 'events'),
                // widget options
                array(
                    'description' => __('Select the Event name.',
                            'events')
               )
        ); //parent __construct()
    }//function __construct()

    public function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['eventsdropdown'] = strip_tags($new_instance['eventsdropdown']);
        return $instance;
    }
// function update

    public function widget($args, $instance) {
        // kick things off
        extract($args);
    }//function widget
    // run a query if on a page
    public function gdd_Events_Register_Widget() {
        register_widget('gdd_Events_Widget');
    }


public function form( $instance ) {

    if ( isset( $instance[ 'eventsdropdown' ] ) )
    $selectedvalue = $instance[ 'eventsdropdown' ];
    else
    $selectedvalue = __( '', 'text-domain' );

    global $event;
    global $events;       
    global $wpdb;  
    global $depth;

     $events = $wpdb->get_results("SELECT id, event, description FROM mo_events order by event",ARRAY_A);
     ?>

     <label for="<?php echo $this->get_field_id('event'); ?>">Select an Event</label> <br>
     <select class="dropdown" id="eventsdropdown" name="eventsdropdown" title="Events Dropdown">

    <?php  
     foreach ($events as $event) {?>
             <option value=" <?php echo $event['id'] ?>"><?php echo $event['event']; ?></option> 
    <?php
    }   ?>
    </select> 
    <br><br> 

    <label for="newevent">Enter a new event name:</label><br>
    <input type="text"id="newevent" newevent="newevent" width="100"</input><br>
    <label for="newevent">Enter a short description:</label><br>
    <input type="textarea" id="neweventdescr" newevent="neweventdescr" width ="400" height="200"</input><br><br> <?
  } // function form  
} //class

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.