简体   繁体   中英

Create select control with php and wordpress

I would like to create a field of selection with a suite of sales save in my database. but I do not know how to put make.

In MySQL my meta_value is: 100,200,300,400

I want to do this:

 <select> 
 <option> 100 </ option>
 <option> 200 </ option>
 <option> 300 </ option>
 <option> 400 </ option>
 </select>

I have tried the following:

 <?php

 function woocommerce_quantity_input() {
 global $post,$product;
 $truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);
 $defaults = array( $truc );

 $options .= '<option value="' . $truc . '">' . $truc . '</option>';

 echo '<select class="qty">' . $options . '</select>';
 }
 ?>

assuming $truc = "100,200,300,400" , you can use explode:

 function woocommerce_quantity_input() {
     global $post,$product;
     $truc = get_post_meta($post->ID, 'woo_best_drop_down_text', true);

     $values = explode(',',$truc);

     echo "<select class='qty'>";
     foreach($values as $v) {
         echo "<option value='$v'>$v</option>";
     }
     echo "</select>";


 }

small pro-tip: if you use double quotes ( "" ) instead of single quotes ( '' ) for strings, you can insert variables directly instead of having to concatenate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM