构建带有选定项目的组合框 [英] Build Combobox with Item Selected

查看:131
本文介绍了构建带有选定项目的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个选择了用户当前值的组合框。我认为我的问题是撇号和引号 - 任何人都可以锐利的眼睛帮助?变量$ MCI在任何引号/撇号之前创建并且正常工作。

Trying to create a combo box with the user's current value selected. I'm thinking my issue is with the apostrophe's and quotes - can anyone with a sharp eye help? Variable $MCI is created before any quotes/apostrophes and is functioning properly.

$MCI = '';
$MCI = $row['MobileCarrierID'];

echo '
<select name="MobileCarrierName">
<?php 
$sql = mysqli_query("SELECT MobileCarrierID, MobileCarrierName FROM tblMobileCarrier ORDER BY MobileCarrierName;");
while ($row = mysqli_fetch_array($sql)){
$MCISelected = (' . $MCI . '==$row["MobileCarrierID"] ? " selected" : "");
echo "<option value=" . $row['MobileCarrierID'] . $MCISelected . ">" . $row['MobileCarrierName'] . "</option>";
}
?>
</select>';

谢谢!

推荐答案

您有

echo '
<select name="MobileCarrierName">
<?php 
$sql=

echo '<select name="MobileCarrierName">';
$sql= 

此外

$MCISelected = (' . $MCI . '==$row["MobileCarrierID"] ? " selected" : "");

需要更改为

$MCISelected = ($MCI==$row["MobileCarrierID"])? " selected" : "";

您的mysqli_query缺少数据库连接,

And your mysqli_query is missing the database connection ie

mysqli_query($db,$query);

最后,关闭

echo '</select>';

您的引号和括号已关闭,您已将所选变量放在选项中的值内,一个完整的编辑代码应该看起来像...

Your quotes and brackets are off and you've placed your selected variable inside your value in the option, a complete edited code should look something like...

<?php
echo '<select name="MobileCarrierName">';
$sql = mysqli_query($conn, "SELECT MobileCarrierID, MobileCarrierName FROM tblMobileCarrier ORDER BY MobileCarrierName;");
while ($row = mysqli_fetch_array($sql)){
$MCISelected = ($MCI==$row["MobileCarrierID"])? " selected" : "";
echo '<option '.$MCIselected.' value="'.$row["MobileCarrierID"].'">'.$row["MobileCarrierName"].'</option>';
}
echo'</select>';

查看如何将选项从具有不同值的多个选项或数组设置为在选择框中选择的视图PHP ,了解其工作原理。

Check out How to set an option from multiple options or array with different values to views as selected in select box using PHP for a guide on how it works

这篇关于构建带有选定项目的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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