create drop down menu in theme customizer from custom db

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.

Read More

thanks for help

Related posts

2 comments

  1. 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>'."nn";
    
    endif;
    
  2. 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>'."nn";
        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')
                        )));    
                        }
    

Comments are closed.