gpt4 book ai didi

php - 重复下拉框 - 总共 20 个 - Mysql - PHP

转载 作者:行者123 更新时间:2023-11-29 05:12:10 27 4
gpt4 key购买 nike

我有 20 个下拉框来查询单个表和相同的字段,我编写了以下查询,效果很好。我确信有某种方法可以优化这种重复的下拉框,但我并不知道。

我应该写一个函数并从每个 <select><? functioname() ?></select> 传递参数吗? enter image description here

<div class="form-group">
<label>Split 1</label>
<select class="form-control required" name='distance[]' id='distance' >
<option value="-1">Select Distance</option>
<?php
$plan_sql="select * from mytable";
$plan_res=mysqli_query($con,$plan_sql);
while($row=mysqli_fetch_array($plan_res,MYSQL_BOTH))
{
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label>Split 2</label>
<select class="form-control required" name='distance[]' id='distance' >
<option value="-1">Select Distance</option>
<?php
$plan_sql="select * from mytablename";
$plan_res=mysqli_query($con,$plan_sql);
while($row=mysqli_fetch_array($plan_res,MYSQL_BOTH))
{
?>
<option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>
<?php
}
?>
</select>
</div>

enter image description here

最佳答案

<?php

// Run your query once and store the results

$plan_sql = "select id, name from mytable";
$plan_res = mysqli_query($con, $plan_sql);
while ($row = mysqli_fetch_array($plan_res, MYSQLI_ASSOC)) {
$result[] = $row;
};

// then you can add your selects without running the query repeatedly

for ($i=1; $i <= 20; $i++):?>
<div class="form-group">
<label>Split <?php echo $i; ?></label>
<select class="form-control required" name='distance[]' id='distance' >
<option value="-1">Select Distance</option>
<?php foreach ($result as $row): ?>
<option value="<?php echo $row['id'];?>"><?php echo $row['name'];?></option>
<?php endforeach ?>
</select>
</div>
<?php endfor; ?>

关于php - 重复下拉框 - 总共 20 个 - Mysql - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37888434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com