drp935159 2016-12-31 14:44
浏览 32

如何从php创建select

I am trying to create some code to do a list of components in mysql.
Something like this

decod - 1,2,3,4,5

and to make it in drop-down listlike this

decod (dorplist) 1
2
3
4
5

I tried this one:

<?php
$mysqli = mysqli_connect("host", "username", "password", "name");

$result = mysqli_query($mysqli, 'SELECT * FROM componnets') or die(mysqli_error(mysqli));



while ($row = mysqli_fetch_array($result)) {
    $org = str_replace("," , "'>", $row['fill']."");
    $replace = str_replace("," , "
    ".$org."</option><option value='", $row['fill']."'>");

echo "<input type='hidden' name='compo' value='".$row['compon']."'>".$row['compon']."";
echo "<select name='fill'>";
echo "<option value='".$replace."'>".$replace."</option>";
echo "</select>";
}

// Free result set
mysqli_free_result($result);
/* close connection */
$mysqli->close();
?>

and do make it output to an XML

but I don't know how! I output like this

<input type='hidden' name='compo' value='DECODERS'>DECODERS
<select name='fill'>
<option value='1010'>20'>30'>40</option>
<option value='2010'>20'>30'>40</option>
<option value='3010'>20'>30'>40</option>
<option value='40'>'>1010'>20'>30'>40</option>
<option value='2010'>20'>30'>40</option>
<option value='3010'>20'>30'>40</option><option value='40'></option>
</select>
<input type='hidden' name='compo' value='DECODERS'>DECODERS
<select name='fill'>
<option value='10'>10</option>
</select>
  • 写回答

1条回答 默认 最新

  • doutan5798 2016-12-31 15:12
    关注

    First create the <select> outside the while loop. Then iterate over the rows to add the elements.

    I'm not sure what you're trying to accomplish with your str_replace calls, so I removed them.

    Something like this might get you started in the right direction:

    <?php
    
    $result = mysqli_query($mysqli, 'SELECT * FROM componnets') or die(mysqli_error(mysqli));
    
    echo "<select name='fill'>";
    echo "<input type='hidden' name='compo' value='".$row['compon']."'>".$row['compon']."";
    while ($row = mysqli_fetch_array($result)) {    
    echo "<option value='".$row['fill']."'>".$row['fill']."</option>";
    }
    echo "</select>";    
    mysqli_free_result($result);
    $mysqli->close();
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行