gpt4 book ai didi

php - 在下拉菜单中设置所选项目php mysql

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

我正在创建一个页面,我想在其中显示从数据库捕获的信息。我想显示的项目之一是从下拉列表菜单中选择的值,但默认为选择的 coverid

我使用以下函数来显示下拉列表:

//function to display the cover in the dropdown
//function to display the cover in the dropdown
public function coverdropdown($companyid, $coverid)
{

$mydata = mysql_query("SELECT a.`coverid`, a.`cover_name`, a.`sum_insured`, a.`info`
FROM covertype a WHERE a.`companyid`=$companyid");
while($record = mysql_fetch_array($mydata))
{
echo '<option value="'.$record['coverid'].'" if ($record['coverid']==$coverid) {selected=selected}">'.$record['cover_name'].' - R'.$record['sum_insured'].'</option>';
}
}

我希望下拉列表显示所选的 coverid。我知道它是这样的:

<option value="" selected="selected">XYZ cover</option>

但现在我不确定如何将它添加到这一行,因为我添加的 if 语句不起作用,它的语法不正确:

while($record = mysql_fetch_array($mydata))
{
echo '<option value="'.$record['coverid'].'" if ($record['coverid']==$coverid) {selected=selected}">'.$record['cover_name'].' - R'.$record['sum_insured'].'</option>';
}

最佳答案

您应该能够在 <option> 之前分配一个变量.类似于:

while($record = mysql_fetch_array($mydata)) {
// One thing to note: In XHTML the selected attribute should probably be defined as <option selected="selected">.
// In regular HTML <option selected> is ok
$selected = ($record['coverid'] == $coverid)? ' selected="selected"':'';
echo '<option value="'.$record['coverid'].'"'.$selected.'>'.$record['cover_name'].' - R'.$record['sum_insured'].'</option>';
}

关于php - 在下拉菜单中设置所选项目php mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27884590/

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