使用相同的mysql查询填充多个下拉列表 [英] Populate more than one drop down using the same mysql query

查看:104
本文介绍了使用相同的mysql查询填充多个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个投币箱的表格,投币箱中的数字为1-5.我可以使用此代码填充下拉列表,但想知道是否可以以某种方式仅对数据库进行一次调用,却填充所有使用相同数字的下拉列表?

I have form with a number of drop boxes which have the numbers 1-5 in them. I can use this code to populate a drop down but was wondering if I can somehow only make the call to the db once but populate all the drop downs that use the same numbers?

    <?php                         

     $sql = "SELECT * FROM riskNumDrop";
     $result = $conn->query($sql);
     if (!$conn->query($sql)) {
     echo "query failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    echo '<select  class="assess" name="precontcons"   style="width:4em">' ;
    while($row = $result->fetch_assoc()){echo '<option value='.   $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
    ?> </select>

因此,理想情况下,我只生成一次输出,然后多次重用.我正在猜测一个数组(已经有$ result了),但是我如何从中填充一个下拉列表呢? TIA

So Ideally, I generate the output once and reuse it multiple times. Im guessing an array (which $result already is) but how do I populate a drop down from it? TIA

推荐答案

另存为字符串可以节省处理多次遍历相同数据并生成相同输出的处理.如果这是您想要的,则可以执行以下操作.

Saving as a string would save you the processing of having to loop through the same data generating the same output multiple times. If this is what you want, you could do the following.

替换:

echo '<select  class="assess" name="precontcons"   style="width:4em">' ;
while($row = $result->fetch_assoc()){echo '<option value='.   $row['riskNumDrop'] .'>'.$row['riskNumDrop'].'</option>';}
?> </select>

具有:

$drop = '<select  class="assess" name="precontcons"   style="width:4em">' ;
while($row = $result->fetch_assoc()){
  drop .= '<option value='.   $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}
$drop .= '</select>';

然后,如果需要,您可以多次echo $drop.

Then you can echo $drop several times if you want.

如果出于任何原因想要不同的选择属性,则可以保存选项列表并在其周围打印选择内容,如下所示:

If for whatever reason you want different select attributes, you could just save the options list and print the select around that, like this:

$dropOptions = "";
while($row = $result->fetch_assoc()){
  $dropOptions .= '<option value='.   $row['riskNumDrop'].'>'.$row['riskNumDrop'].'</option>';
}

然后只是echo '<select class="foo" name="bar">'.$dropOptions.'</select'>

这篇关于使用相同的mysql查询填充多个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆