Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

what i am trying to do is create drop down menu in theme customizer from custom db i have created. db name "students". in the db i have only the id and the name. i want to show all students names in the drop down menu. and when the user will choose some of the students name and will click on the save button (i need to save his choose), and i want that next time the same name will be selected. in this drop down menu.

i tried to make this work but without any results.

thanks for help

share|improve this question
add comment

2 Answers

Assuming that you mean that students is a table within your WP database, this should get you started.

I'd recommend a read of the following materials, which are pretty useful -

Here is the code to add the dorpdown box -

/** Required to make use of the wpdb Class */
global $wpdb;

/** Query the database */
$query = $wpdb->prepare('SELECT * FROM %1$s ORDER BY ID DESC', 'students');
$results = $wpdb->get_results($query);

/** Check for $results */
if(!empty($results)) :

    /** Loop through the $results and add each as a dropdown option */
    $options = '';
    foreach($results as $result) :

        $options.= sprintf("\t".'<option value="%1$s">%2$s</option>'."\n", $result->ID, $result->name);

    endforeach;

    /** Output the dropdown */
    echo '<select id="my-select" name="my-select">'."\n";
    echo $options;
    echo '</select>'."\n\n";

endif;
share|improve this answer
    
Hi @DavidGard, strange but i get error: Parse error: syntax error, unexpected '=' in 12 ($options = '';). I can not understand where is the problem in the code. –  someone Jul 31 '13 at 15:38
    
I accidently added an extra space. Change $options. = to $options.=. –  David Gard Jul 31 '13 at 15:41
    
Thanks @DavidGard for the help now i get all the name in the drop down. but when i choose one of the students names and click on the save button it not remmber my choose. i need it to be selected but i can not make it work. thanks. –  someone Jul 31 '13 at 15:58
    
someone can help? –  someone Aug 1 '13 at 4:51
    
Please see my edit. I've added a link to the Wordpress Theme Customisation API, where it explains exactly how to do what you require. –  David Gard Aug 1 '13 at 9:59
show 1 more comment


here is the code i am tring to make it work.

1. here i am tring to pull out the selected opsion. but this not working. evry time the user choose some student name and click the save button the selected opsion returns to be default opsion.

2. i have create class to insert this drop down menu to Theme Customizer section but it not working and i get php error.

what i do wrong?

Thanks for help!

 if (class_exists('WP_Customize_Control')): 
    class Selected_students_Control extends WP_Customize_Control {

    global $wpdb;
    $query = $wpdb->prepare('SELECT * FROM %1$s ORDER BY ID DESC', 'l7b8z_students');
    $results = $wpdb->get_results($query);
    if(!empty($results)) :
        $options = '';
        foreach($results as $result) :
        $is_selected = (get_the_title() == $selected) ? 'selected="selected"' : '';
            $options.= sprintf("\t".'<option value="%1$s $is_selected">%2$s</option>'."\n", $result->id, $result->name);

        endforeach;

        echo '<select id="my-select" name="my-select">'."\n";
        echo $options;
        echo '</select>'."\n\n";
    endif;

       }
    endif;


    public function customize($customize) {

            $customize->add_setting('students-admin', array(
                'default' => 'Students Admin',
            ));

            $customize->add_control(new Selected_students_Control

    ($customize, 'students-admin', array(
                        'label' => "Select Default ThemeAdmin",
                        'section' => 'footer_section',
                        'settings' => 'students-admin',
                        'description' => 'some text',
                        'query' => array('result' => 'name')
                    )));    
                    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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