组合框不会显示MySQL的值

组合框不会显示MySQL的值

问题描述:

I am trying to fill a combo box from a custom table in Wordpress. print_r(array_values($cellco_options)) returns the expected array of 15 items. My foreach statement is a problem. If I echo the variables the combobox will not fill with items. If I remove the echo the combox fills with 15 items but they are blank.

Questions: Why is echo not allowing the box to fill? How can I fill my box with items I can see, is echo the wrong method in this case?

    <?php 
    global $wpdb;
    $cellco_options=$wpdb->get_results("SELECT `id`, `cellco` FROM `ea_cellcarrier` WHERE 1"); 
    ?>

    <p>
        <label for="cell_carrier<?php $template->the_instance(); ?>"><?php _e( 'Cell Carrier', 'theme-my-login' ) ?></label>

        <select type="text" name="cell_carrier" id="cell_carrier<?php $template->the_instance(); ?>" tabindex="20" />
            <option disabled selected value=""> -- select -- </option>
                <?php 
                    foreach ($cellco_options as $id => $cellco) { ?>
                    <option value= "<?php echo $id; ?>"><?php echo $cellco; ?></option>
                <?php   }
                ?>
        </select>           
    </p>

我正在尝试从Wordpress中的自定义表填充组合框。 print_r(array_values($ cellco_options)) code>返回15个项目的预期数组。 我的 foreach code>语句是个问题。 如果我回显变量,组合框将不会填充项目。 如果我删除回声,则combox会填充15个项目,但它们是空白的。 p>

问题: 为什么echo不允许填充框? 如何填写我的框中的项目? 在这种情况下可以看到,回显错误的方法吗? p>

 &lt;?php 
 global $ wpdb; 
 $ cellco_options = $ wpdb-&gt; get_results(“SELECT  `id`,`cellco` FROM`ea_cellcarrier` WHERE 1“);  
?&gt; 
 
&lt; p&gt; 
&lt; label for =“cell_carrier&lt;?php $ template-&gt; the_instance();?&gt;”&gt;&lt;?php _e('Cell Carrier'  ,'theme-my-login')?&gt;&lt; / label&gt; 
 
&lt; select type =“text”name =“cell_carrier”id =“cell_carrier&lt;?php $ template-&gt; the_instance();  ?&gt;”中 tabindex =“20”/&gt; 
&lt; option disabled selected value =“”&gt;  - 选择 - &lt; / option&gt; 
&lt;?php 
 foreach($ cellco_options as $ id =&gt; $ cellco){?&gt; 
&lt; option value =“&lt;?php echo $ id  ;&gt;“&gt;&lt;?php echo $ cellco;  ?&gt;&lt; / option&gt; 
&lt;?php} 
?&gt; 
&lt; / select&gt;  
&lt; / p&gt; 
  code>  pre> 
  div>

Thank you miken32, I tried your tip and still had the blank spaces, however, your tip led to the following solution that works:

    global $wpdb;
    $cellco_options=$wpdb->get_results("SELECT `id`, `cellco` FROM `ea_cellcarrier` WHERE 1"); 
    ?>

    <p>
        <label for="cell_carrier<?php echo $template->the_instance(); ?>"><?php _e( 'Cell Carrier', 'theme-my-login' ) ?></label>
        <select type="text" name="cell_carrier" id="cell_carrier<?php echo $template->the_instance(); ?>" tabindex="20">
            <option selected value="" > -- select -- </option>
                <?php foreach ($cellco_options as $option):?>
                    <option value="<?php echo $option->id; ?>" >
                    <?php echo $option->cellco; ?></option>
                <?php endforeach; ?>
        </select>
    </p>

Your database results are not indexed by id so your foreach loop was not getting what you expected.

Also note the alternative syntax I used with the foreach() loop. It can be much easier to read in cases like this where you're mixing your PHP with HTML.

<?php 
global $wpdb;
$cellco_options=$wpdb->get_results("SELECT `id`, `cellco` FROM `ea_cellcarrier`", ARRAY_N); 
?>

<p>
    <label for="cell_carrier<?php echo $template->the_instance(); ?>"><?php _e( 'Cell Carrier', 'theme-my-login' ) ?></label>
    <select type="text" name="cell_carrier" id="cell_carrier<?php $template->the_instance(); ?>" tabindex="20">
        <option disabled="disabled" selected="selected" value=""> -- select -- </option>
<?php foreach ($cellco_options as $option):?>
        <option value= "<?php echo $option[0]?>"><?php echo $option[1]?></option>
<?php endforeach;?>
    </select>           
</p>

Your select html tag is closed at the end of the line.

And you can use php alternative syntax for the code.

  <?php 
    global $wpdb;
    $cellco_options=$wpdb->get_results("SELECT `id`, `cellco` FROM `ea_cellcarrier` WHERE 1"); 
    ?>

    <p>
        <label for="cell_carrier<?php echo $template->the_instance(); ?>"><?php _e( 'Cell Carrier', 'theme-my-login' ) ?></label>

        <select type="text" name="cell_carrier" id="cell_carrier<?php echo $template->the_instance(); ?>" tabindex="20">
            <option disabled selected value=""> -- select -- </option>
                <?php foreach ($cellco_options as $id => $cellco): ?>
                    <option value="<?php echo $id; ?>"><?php echo $cellco; ?></option>
                <?php endforeach; ?>
        </select>
    </p>