简体   繁体   English

在 PHP 变量中填充下拉列表

[英]Populating drop down list in the PHP variable

    while ($row = mysqli_fetch_array($result)){
      $move = '
        <select type = text name="mover">
         <option>select something</option>
          '".while ($dbrow = mysqli_fetch_array($result)){
              echo "<option value='".$dbrow['deptname']."'>".$dbrow['deptname']."</option>";}."'
        </select>';

    echo '<tr><td>'.$row['name'].'</td><td>'.$move.'</td></tr>';}

I am trying to display dropdown list inside the table.我正在尝试在表格内显示下拉列表。 However whenever I apply while(...) codes, it displays an error.但是,每当我应用while(...)代码时,它都会显示错误。

shouldn't HTML + '".PHP."' + HTML be the correct way? HTML + '".PHP."' + HTML不应该是正确的方法吗?

Don't concatenate.不要串联。 End your string, start your second while loop.结束你的字符串,开始你的第二个 while 循环。

while ($row = mysqli_fetch_array($result)){
  $move = '<select name="mover">
    <option>select something</option>';
    while ($dbrow = mysqli_fetch_array($result)){
      $move .= "<option value='".$dbrow['deptname']."'>".$dbrow['deptname']."</option>";
    }
    $move .= '</select>';
    echo '<tr><td>'.$row['name'].'</td><td>'.$move.'</td></tr>';
}

Removed type=text from your <select> .从您的<select>中删除type=text

Replaced your echo s with $move.=... to add to the $move variable.echo替换$move.=...以添加到$move变量中。

Be aware that, depending on the number of items in $row this would leave your HTML with multiple <select> s with the same name .请注意,根据$row中的项目数量,这将使您的 HTML 具有多个具有相同name<select>

When you use " the php is resolved so you just need to escape the html " s当您使用" php 已解决,因此您只需要转义 html "

while ($row = mysqli_fetch_array($result)) { 
  echo "<tr><td>$row['name']</td><td><select name=\"mover\"><option value=\"\">select something</option>";
  while ($dbrow = mysqli_fetch_array($result))
      echo "<option value=\"$dbrow['deptname']\">$dbrow['deptname']</option>";
  echo "</select></td></tr>";
}

Generate the select menu before entering the main loop that builds the table and then reqind the recordset so that the main loop can begin在进入构建表的主循环之前生成select菜单,然后重新生成记录集,以便可以开始主循环

<?php
    $html='<select name="mover">
        <option selected disabled hidden>Please select something';
        
    while( $row = mysqli_fetch_array( $result ) ){
        $html.=sprintf('<option>%s',$row['deptname'] );
    }
    
    $html.='</select>';
    $result->data_seek(0);
?>

Then build the table.然后建表。

<table>
<?php
    while( $row = mysqli_fetch_array( $result ) ){
        printf('<tr><td>%1$s</td><td>%2$s</td></tr>',$row['deptname'],$html);
    }
?>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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