具有多个选择选项的选择列表中的PHP回显

问题描述:

I'm having a bit of struggle putting together a selection list in HTML where the options or values are being retrieved from a table in the database. I get the following result: Select List

As you can see this is not my intention, I rather have all options in 1 Selection List, with multiple options clickable.

This is my HTML Code (Bootstrap Framework):

<div class="container-fluid well well-lg">

     <?php while ($itemsrow = $query->fetch()) : ?>

<div class="form-group">

      <label for="sel1">Select list:</label>

<select class="form-control" id="sel1">

         <option><?php echo $itemsrow['Beschrijving']; ?></option>

The PHP Logic:

$conn = Db::getInstance();
$query = $conn->prepare("SELECT * FROM items WHERE user_id = $userID");
$query->execute();

Thanks in advance!

我正在努力将HTML中的选择列表放在一起,从中检索选项或值 数据库中的表。 我得到以下结果: p>

正如您所看到的,这不是我的意图,我宁愿拥有1个选择列表中的所有选项, 多个选项可点击。 p>

这是我的HTML代码(Bootstrap框架): p>

 &lt; div class =“container-fluid well well  -lg“&gt; 
 
&lt;?php while($ itemsrow = $ query-&gt; fetch()):?&gt; 
 
&lt; div class =”form-group“&gt; 
 
  &lt; label for =“sel1”&gt;选择列表:&lt; / label&gt; 
 
&lt; select class =“form-control”id =“sel1”&gt; 
 
&lt; option&gt;&lt;?php  echo $ itemsrow ['Beschrijving'];  ?&gt;&lt; / option&gt; 
  code>  pre> 
 
 

p>

PHP逻辑: p>

  $ conn = Db :: getInstance(); 
 $ query = $ conn-&gt; prepare(“SELECT * FROM items WHERE user_id = $ userID”); 
 $ query-  &gt;执行(); 
  code>  pre> 
 
 

提前致谢! p> div>

Currently, you're creating new elements inside the loop so yes, you will get a new select per iteration.

You should only keep the <option>-elements inside the loop:

<div class="container-fluid well well-lg">
    <div class="form-group">
        <label for="sel1">Select list:</label>

            <select class="form-control" id="sel1">

            <?php while ($itemsrow = $query->fetch()) : ?>  

                <option><?php echo $itemsrow['Beschrijving']; ?></option>

            <?php endwhile; ?>

You should put your loop into <select> tag

<select class="form-control" id="sel1">
    <?php while ($itemsrow = $query->fetch()) : ?>
        <option><?php echo $itemsrow['Beschrijving']; ?></option>