如何创建填充了下拉列表的小部件?

时间:2020-04-30 作者:GreatDayDan

(添加了代码;我想我已经在这里了)我正在尝试构建一个具有下拉列表的小部件。

   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   
我不知道这里发生了什么。我的查询是:SELECT id,event FROM$wpdb->mo\\u event,但调试日志显示:调试。日志显示:从[2020年4月30日15:35:04 UTC]WordPress数据库错误中选择事件您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解在第1行的“”附近使用的查询选择事件的正确语法,该事件由wp\\u list\\u widgets、wp\\u widget\\u control、call\\u user\\u func\\u array、wp\\u widget->form\\u callback、gdd\\u Events\\u widget->form

插件激活,小部件有一个小的空下拉列表。

有人能帮我把这件事做好吗,这样我就可以制作一个合适的小部件了?

谢谢Dan\'l公司

1 个回复
SO网友:GreatDayDan

我终于构建了这个小部件。我希望这能帮助其他新手。

<?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