【问题标题】:Populate html <select> with array data from mysql in PHP在 PHP 中使用来自 mysql 的数组数据填充 html <select>
【发布时间】:2013-09-22 05:07:25
【问题描述】:

我可以看到查询返回结果,但我似乎无法将它们放入 html 下拉框中。此外,下拉框中的条目与查询返回的条目一样多,但它们都是空白。但是,页面源显示正确的选项值,例如

<option value="3 John"></option>

<option value="Jude"></option>

<option value="Revelation"></option>

有人可以帮帮我吗?为什么它们实际上不显示在下拉框中?

<html>
<?php
    //Connect to the database
    $mysqli = new mysqli("localhost", "root", "", "bible");

    //Return an error if we have a connection issue
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
        }

    //Query the database for the results we want
    $query = $mysqli->query("select distinct bname as Name from kjv limit 1");

    //Create an array  of objects for each returned row
    while($array[] = $query->fetch_object());

    array_pop($array);

    //Print out the array results
    print_r($array);
    ?>
    <h3>Dropdown Demo Starts Here</h3>
    <select name="the_name">
    <?php foreach($array as $option) : ?>
        <option value="<?php echo $option->Name; ?>"></option>
    </select>
        <?php endforeach; ?>

【问题讨论】:

    标签: php html mysqli


    【解决方案1】:

    试试这个

    <select name="the_name">
    <?php foreach($array as $option) : ?>
            <option value="<?php echo $option['Name']; ?>"><?php echo $option['Name']; ?></option>
    <?php endforeach; ?>
    </select>
    

    【讨论】:

    • 你是对的,他正在打印属性中的值。 @Slava Basat 在选项标签中打印它。
    • 谢谢,但是在选项标签中打印什么?
    • 一个小问题,但代码将在没有 最后。我怀疑@Reshil 是从另一种语言(如 vba)中继承下来的,实际上有必要用它来结束一个 foreach 循环。
    【解决方案2】:

    查询执行后,使用while循环添加选项来选择

    $query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
    
    <select>
        <?php while($option = $query->fetch_object()){ ?>
            <option><?php echo $option->Name; ?></option>
        <?php } ?>
    </select>
    

    不确定array_pop在代码中做了什么

    【讨论】:

    • 您好,我现在就试试这个。数组 pop 从查询结果中删除最后一个空记录。我在 youtube 上看到了,否则我永远不会放在那里
    • 你是一个 G. 这个工作尊重的人
    【解决方案3】:

    正如 TIM WAX 所说,这是解决方案

    $query = $mysqli->query("select distinct bname as Name from kjv limit 1"); ?>
    
    <select>
        <?php while($option = $query->fetch_object()){ ?>
            <option><?php echo $option->Name; ?></option>
        <?php } ?>
    </select>
    

    【讨论】:

      【解决方案4】:
      <select name="the_name">
      <?php foreach($array as $option) : ?>
              <option value="<?php echo $option->Name; ?>"></option>
      <?php endforeach; ?>
      </select>
      

      你结束了你的循环,它也一次又一次地创建&lt;select&gt;标签。更改它并重试。我对.php 了解不多,但显示您的下拉框可能是个问题。

      【讨论】:

      【解决方案5】:

      这是我的 .. 我是初学者,但它对我有用,

          $query = $mysqli->query("SELECT * FROM  `student_type_db`"); //table of student type
      
          echo "<select>";
          while($row = $query->fetch_array()){
               echo "<option>";
               echo $row['student_type'] . " - " . $row['student_description'];
               echo "</option>"; 
          }
          echo "</select>";
      
         // student type = 1 | student description = regular
         // output : 1 - regular
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-19
        • 2017-09-07
        • 2014-03-10
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 2020-08-07
        • 2017-11-06
        相关资源
        最近更新 更多