Populate woocommerce_wp_select field from database












0















please help me out with this..I've tried too many different things...
I'm trying to populate a woocommerce select field with data from the database.



// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);


add_action( 'woocommerce_product_data_panels',
'wcpt_roller_blind_options_product_tab_content' );

function wcpt_roller_blind_options_product_tab_content() {

?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $array
));

?></div>
</div><?php


Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.










share|improve this question



























    0















    please help me out with this..I've tried too many different things...
    I'm trying to populate a woocommerce select field with data from the database.



    // Add fields & settings to the custom product tab
    $SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
    $array = $wpdb->get_results( $SQL, ARRAY_A);


    add_action( 'woocommerce_product_data_panels',
    'wcpt_roller_blind_options_product_tab_content' );

    function wcpt_roller_blind_options_product_tab_content() {

    ?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
    ?><div class='options_group'><?php
    woocommerce_wp_select( array(
    'id' => 'roller_blind_tables',
    'label' => __( 'Price Tables', 'wcpt' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Select Associated Price Table.', 'wcpt' ),
    'options' => $array
    ));

    ?></div>
    </div><?php


    Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.










    share|improve this question

























      0












      0








      0








      please help me out with this..I've tried too many different things...
      I'm trying to populate a woocommerce select field with data from the database.



      // Add fields & settings to the custom product tab
      $SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
      $array = $wpdb->get_results( $SQL, ARRAY_A);


      add_action( 'woocommerce_product_data_panels',
      'wcpt_roller_blind_options_product_tab_content' );

      function wcpt_roller_blind_options_product_tab_content() {

      ?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
      ?><div class='options_group'><?php
      woocommerce_wp_select( array(
      'id' => 'roller_blind_tables',
      'label' => __( 'Price Tables', 'wcpt' ),
      'placeholder' => '',
      'desc_tip' => 'true',
      'description' => __( 'Select Associated Price Table.', 'wcpt' ),
      'options' => $array
      ));

      ?></div>
      </div><?php


      Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.










      share|improve this question














      please help me out with this..I've tried too many different things...
      I'm trying to populate a woocommerce select field with data from the database.



      // Add fields & settings to the custom product tab
      $SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
      $array = $wpdb->get_results( $SQL, ARRAY_A);


      add_action( 'woocommerce_product_data_panels',
      'wcpt_roller_blind_options_product_tab_content' );

      function wcpt_roller_blind_options_product_tab_content() {

      ?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
      ?><div class='options_group'><?php
      woocommerce_wp_select( array(
      'id' => 'roller_blind_tables',
      'label' => __( 'Price Tables', 'wcpt' ),
      'placeholder' => '',
      'desc_tip' => 'true',
      'description' => __( 'Select Associated Price Table.', 'wcpt' ),
      'options' => $array
      ));

      ?></div>
      </div><?php


      Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.







      mysql wordpress select woocommerce






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 28 '18 at 18:00









      user2687383user2687383

      33




      33
























          2 Answers
          2






          active

          oldest

          votes


















          0















          1. You need first to be sure of table_name or replace it by the correct column slug to be queried from your database table wp_lbc_prices.

          2. You need to replace the WPDB method get_results() by get_col()which query a single column and gives an array natively.

          3. Prepare the array to copying the values to the keys using array_combine()


          I have completed your code with the hooked function that adds a custom product tab.



          Your revisited code will be:



          add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
          function add_roller_blind_product_data_tab( $tabs ) {
          $tabs['roller_blind'] = array(
          'label' => __( 'Roller blind', 'wcpt' ),
          'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
          //'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
          );

          return $tabs;
          }

          add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
          function display_roller_blind_product_data_tab_content() {
          global $wpdb;

          echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
          <div class="options_group">';

          $sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
          $options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();

          woocommerce_wp_select( array(
          'id' => 'roller_blind_tables',
          'label' => __( 'Price Tables', 'wcpt' ),
          'placeholder' => '',
          'desc_tip' => 'true',
          'description' => __( 'Select Associated Price Table.', 'wcpt' ),
          'options' => $options
          ));

          echo '</div></div>';
          }


          It should better work now. But as this is a custom database table is not testable.






          share|improve this answer
























          • Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.

            – user2687383
            Nov 29 '18 at 4:41





















          0














          get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object) can you please share what you are getting in $array variable ?






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525467%2fpopulate-woocommerce-wp-select-field-from-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0















            1. You need first to be sure of table_name or replace it by the correct column slug to be queried from your database table wp_lbc_prices.

            2. You need to replace the WPDB method get_results() by get_col()which query a single column and gives an array natively.

            3. Prepare the array to copying the values to the keys using array_combine()


            I have completed your code with the hooked function that adds a custom product tab.



            Your revisited code will be:



            add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
            function add_roller_blind_product_data_tab( $tabs ) {
            $tabs['roller_blind'] = array(
            'label' => __( 'Roller blind', 'wcpt' ),
            'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
            //'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
            );

            return $tabs;
            }

            add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
            function display_roller_blind_product_data_tab_content() {
            global $wpdb;

            echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
            <div class="options_group">';

            $sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
            $options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();

            woocommerce_wp_select( array(
            'id' => 'roller_blind_tables',
            'label' => __( 'Price Tables', 'wcpt' ),
            'placeholder' => '',
            'desc_tip' => 'true',
            'description' => __( 'Select Associated Price Table.', 'wcpt' ),
            'options' => $options
            ));

            echo '</div></div>';
            }


            It should better work now. But as this is a custom database table is not testable.






            share|improve this answer
























            • Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.

              – user2687383
              Nov 29 '18 at 4:41


















            0















            1. You need first to be sure of table_name or replace it by the correct column slug to be queried from your database table wp_lbc_prices.

            2. You need to replace the WPDB method get_results() by get_col()which query a single column and gives an array natively.

            3. Prepare the array to copying the values to the keys using array_combine()


            I have completed your code with the hooked function that adds a custom product tab.



            Your revisited code will be:



            add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
            function add_roller_blind_product_data_tab( $tabs ) {
            $tabs['roller_blind'] = array(
            'label' => __( 'Roller blind', 'wcpt' ),
            'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
            //'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
            );

            return $tabs;
            }

            add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
            function display_roller_blind_product_data_tab_content() {
            global $wpdb;

            echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
            <div class="options_group">';

            $sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
            $options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();

            woocommerce_wp_select( array(
            'id' => 'roller_blind_tables',
            'label' => __( 'Price Tables', 'wcpt' ),
            'placeholder' => '',
            'desc_tip' => 'true',
            'description' => __( 'Select Associated Price Table.', 'wcpt' ),
            'options' => $options
            ));

            echo '</div></div>';
            }


            It should better work now. But as this is a custom database table is not testable.






            share|improve this answer
























            • Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.

              – user2687383
              Nov 29 '18 at 4:41
















            0












            0








            0








            1. You need first to be sure of table_name or replace it by the correct column slug to be queried from your database table wp_lbc_prices.

            2. You need to replace the WPDB method get_results() by get_col()which query a single column and gives an array natively.

            3. Prepare the array to copying the values to the keys using array_combine()


            I have completed your code with the hooked function that adds a custom product tab.



            Your revisited code will be:



            add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
            function add_roller_blind_product_data_tab( $tabs ) {
            $tabs['roller_blind'] = array(
            'label' => __( 'Roller blind', 'wcpt' ),
            'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
            //'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
            );

            return $tabs;
            }

            add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
            function display_roller_blind_product_data_tab_content() {
            global $wpdb;

            echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
            <div class="options_group">';

            $sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
            $options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();

            woocommerce_wp_select( array(
            'id' => 'roller_blind_tables',
            'label' => __( 'Price Tables', 'wcpt' ),
            'placeholder' => '',
            'desc_tip' => 'true',
            'description' => __( 'Select Associated Price Table.', 'wcpt' ),
            'options' => $options
            ));

            echo '</div></div>';
            }


            It should better work now. But as this is a custom database table is not testable.






            share|improve this answer














            1. You need first to be sure of table_name or replace it by the correct column slug to be queried from your database table wp_lbc_prices.

            2. You need to replace the WPDB method get_results() by get_col()which query a single column and gives an array natively.

            3. Prepare the array to copying the values to the keys using array_combine()


            I have completed your code with the hooked function that adds a custom product tab.



            Your revisited code will be:



            add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
            function add_roller_blind_product_data_tab( $tabs ) {
            $tabs['roller_blind'] = array(
            'label' => __( 'Roller blind', 'wcpt' ),
            'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
            //'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
            );

            return $tabs;
            }

            add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
            function display_roller_blind_product_data_tab_content() {
            global $wpdb;

            echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
            <div class="options_group">';

            $sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
            $options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();

            woocommerce_wp_select( array(
            'id' => 'roller_blind_tables',
            'label' => __( 'Price Tables', 'wcpt' ),
            'placeholder' => '',
            'desc_tip' => 'true',
            'description' => __( 'Select Associated Price Table.', 'wcpt' ),
            'options' => $options
            ));

            echo '</div></div>';
            }


            It should better work now. But as this is a custom database table is not testable.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 28 '18 at 22:44









            LoicTheAztecLoicTheAztec

            95k1370111




            95k1370111













            • Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.

              – user2687383
              Nov 29 '18 at 4:41





















            • Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.

              – user2687383
              Nov 29 '18 at 4:41



















            Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.

            – user2687383
            Nov 29 '18 at 4:41







            Thanks so much! The code works great, and you've given me some additional methods to look into and use for the future. And thanks also for the explainations.

            – user2687383
            Nov 29 '18 at 4:41















            0














            get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object) can you please share what you are getting in $array variable ?






            share|improve this answer




























              0














              get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object) can you please share what you are getting in $array variable ?






              share|improve this answer


























                0












                0








                0







                get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object) can you please share what you are getting in $array variable ?






                share|improve this answer













                get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object) can you please share what you are getting in $array variable ?







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 28 '18 at 19:08









                Waqas AliWaqas Ali

                11




                11






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53525467%2fpopulate-woocommerce-wp-select-field-from-database%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)