我正在寻找一些方法,根据mysql的结果选择php组合框"
实际上,我正在研究一个php表单,该表单将用于编辑mysql表中的现有值。我的第一个表单将简单地传递要编辑的记录的 id,如下所示单击以编辑
editCalendar.php上的代码如下:
<?php
include("dbpath.php");
$id = htmlspecialchars($_GET["id"]);
$sql="Select * from event_Date where eventid=" . $id;
$result=mysql_query($sql);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result)
{
$message = 'Invalid query: ' . mysql_error() . "'n";
$message .= 'Whole query: ' . $query;
die($message);
}
while($row = mysql_fetch_assoc($result))
{
$name=$row["EventTitle"];
$close=$row["OpenOrClose"];
$remarks=$row["Remarks"];
$date=$row["EventDate"];
$type=$row["Type"];
}
?>
在$close
中获得的值将用于在同一表单上选择"选择关闭或打开",即用户将从填充的列表中获得预选选项。
<select id="SelectClosedOrOpen">
<option value="3">Select</option>
<option value="0">Open</option>
<option value="1">Closed</option>
</select>
这意味着,如果$close
有 0,那么必须选择<option value="0">Open</option>
否则如果$close有 1,那么<option value="1">Closed</option>
应该在 formload 上自动选择。
你只需要在那里写selected
属性。 试试这个
<select id="SelectClosedOrOpen">
<option value="3" <?php echo ($close == 3) ? 'selected="selected"': ''; ?>>Select</option>
<option value="0" <?php echo ($close == 0) ? 'selected="selected"': ''; ?>>Open</option>
<option value="1" <?php echo ($close == 1) ? 'selected="selected"': ''; ?>>Closed</option>
</select>
几年前我创建了这个函数。我希望它对你有所帮助。它基本上需要预先选择一组选项和选项的值。它返回您的选择的选项,因此您仍然需要创建 SELECT 标签,这允许您自定义 ID、JS 等。
function drop_down_box_options_from_array($choices,$default="")
{
$output= '';
// $choices is contructed using $choices[]=array("value","Displayed Choice");
while (list ($key, $val) = each ($choices))
{
$output.= '<option value="';
$output.= $choices[$key][0];
if ($default==$choices[$key][0])
{
$output.= '" selected="selected" >';
}
else
{
$output.= '">';
}
$output.= $choices[$key][1];
$output.= '</option>';
$output.= "'n";
}
return $output;
}
使用您的方案:
<select id="SelectClosedOrOpen" name="OpenOrClose">
<?php
// defined here for clarity, but can be defined earlier ie in a config file
$choices[]=array('3', 'Select');
$choices[]=array('0', 'Open');
$choices[]=array('1', 'Closed');
echo drop_down_box_options_from_array($choices, $row['OpenOrClose']);
?>
</select>