• How do I populate a select from the database.

    I have tried to write this widget, but I can’t populate the select- list

    <?php
    /*
    Plugin Name: S?g Side 9 pige
    Description: Adds a sidebar widget to let users search Side 9 pige.
    Author: jeto
    Version: 1.0
    Author URI: https://bjoerchmart.dk
    */

    // Put functions into one big function we’ll call at the plugins_loaded
    // action. This ensures that all required plugin functions are defined.
    function widget_side9_init() {

    // Check for the required plugin functions. This will prevent fatal
    // errors occurring when you deactivate the dynamic-sidebar plugin.
    if ( !function_exists(‘register_sidebar_widget’) )
    return;

    // This is the function that outputs our little Side9 search form.
    function widget_side9($args) {

    // $args is an array of strings that help widgets to conform to
    // the active theme: before_widget, before_title, after_widget,
    // and after_title are the array keys. Default tags: li and h2.
    extract($args);

    // Each widget can store its own options. We keep strings here.
    $options = get_option(‘widget_side9’);
    $title = $options[‘title’];
    $buttontext = $options[‘buttontext’];

    // These lines generate our output. Widgets can be very complex
    // but as you can see here, they can also be very, very simple.
    echo $before_widget . $before_title . $title . $after_title;
    echo ‘<div>
    <form name=”input” action=”jokelist6.php”
    method=”post”>
    <div align=”center”><center>dato: <select name=”Nyid”>
    global $wpdb $row;
    $row = $wpdb->get_results(“SELECT dato FROM wp_side9_1”);

    foreach ($row as $row) {

    print(“<option value=\”$row->dato\;”>echo $row->dato;</option>”);
    }
    </select>  <input type=”submit” value=”V?lg dato”>
    </center></div>
    </form>’;
    echo $after_widget;
    }

    // This is the function that outputs the form to let the users edit
    // the widget’s title. It’s an optional feature that users cry for.
    function widget_side9_control() {

    // Get our options and see if we’re handling a form submission.
    $options = get_option(‘widget_side9’);
    if ( !is_array($options) )
    $options = array(‘title’=>”, ‘buttontext’=>’Side 9 pige’);
    if ( $_POST[‘gsearch-submit’] ) {

    // Remember to sanitize and format use input appropriately.
    $options[‘title’] = strip_tags(stripslashes($_POST[‘side9-title’]));
    $options[‘buttontext’] = strip_tags(stripslashes($_POST[‘side9-buttontext’]));
    update_option(‘widget_side9’, $options);
    }

    // Be sure you format your options to be valid HTML attributes.
    $title = htmlspecialchars($options[‘title’], ENT_QUOTES);
    $buttontext = htmlspecialchars($options[‘buttontext’], ENT_QUOTES);

    // Here is our little form segment. Notice that we don’t need a
    // complete form. This will be embedded into the existing form.
    echo ‘<p style=”text-align:right;”><label for=”side9-title”>Title: <input style=”width: 200px;” id=”side9-title” name=”side9-title” type=”text” value=”‘.$title.'” /></label>’;
    echo ‘<p style=”text-align:right;”><label for=”side9-buttontext”>Button Text: <input style=”width: 200px;” id=”gsearch-buttontext” name=”gsearch-buttontext” type=”text” value=”‘.$buttontext.'” /></label>’;
    echo ‘<input type=”hidden” id=”side9-submit” name=”side9-submit” value=”1″ />’;
    }

    // This registers our widget so it appears with the other available
    // widgets and can be dragged and dropped into any active sidebars.
    register_sidebar_widget(‘Side 9’, ‘widget_side9’);

    // This registers our optional widget control form. Because of this
    // our widget will have a button that reveals a 300×100 pixel form.
    register_widget_control(‘Side 9’, ‘widget_side9_control’, 300, 100);
    }

    // Run our code later in case this loads prior to any required plugins.
    add_action(‘plugins_loaded’, ‘widget_side9_init’);

    ?>

    Can anybody help me ?

  • The topic ‘populating a select form from database’ is closed to new replies.