在我的模态窗口中,我有一个选项,用户可以从我的数据库中选择早餐。通过选择膳食并提交它,记录将保存在数据库中。但是,当用户进入相同的模态窗口时,我希望实现这一点,他们之前选择的任何餐点都将显示为默认选项。我创建了一个查询,检查是否已经选择了一餐(optionquery1)。任何想法实现这一目标?感谢
另一方面,我不确定我是否正在设置日期php变量。日,月和年存储在html隐藏输入中,但不确定如何提取值。
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<div class="modal-body">
<form action="my_planner.php" method="post">
<!--<span class="test5"></span>-->
<!--<span class="test"><input type="text" name="id_disabled" value="" disabled/>-->
<input class="test" type="text" name="id1" value="" style="text-align:center; border:0px; font-weight:bold; font-size: 25px;"/>
<hr class="my-4">
<input type="hidden" name="id" value=""/>
<input type="hidden" name="month" value=""/>
<input type="hidden" name="year" value=""/>
<br>
<p id="breakfastTag"><br>Breakfast:</p>
<select id="breakfast" name="breakfast">
<option value="" disabled selected>Select your breakast</option>
<?
$meal='breakfast';
$date='<input type="hidden" name="id" value=""/>'.'<input type="hidden" name="month" value=""/>'.'<input type="hidden" name="year" value=""/>';
$query = $db->prepare("select * from recipe_category where categoryID=1");
$query->execute();
while ($results = $query->fetch()) {
$recipeID=$results["recipeID"];
$query3 = $db->prepare("select * from recipe where id=:recipeID");
$dbParams3=array('recipeID'=>$recipeID);
$query3->execute($dbParams3);
$breakfast = $query3->fetchAll();
$optionquery = $db->prepare("select * from user_planner where userID=:userID and date=:date and meal=:meal");
$optionParams= array ('userID'=>$thisUser, 'date'=>$date,'meal'=>$meal);
$optionquery->execute($optionParams);
while ($results12 = $optionquery->fetch()) {
$selectedmeal = $results12['recipeID'];
}
$optionquery1 = $db->prepare("select * from recipe where id=:recipeID");
$optionParams1= array ('recipeID'=>$selectedmeal);
$optionquery1->execute($optionParams1);
while ($results123 = $optionquery1->fetch()) {
$selectedrecipe = $results123['name'];
}
foreach($breakfast as $breakfast): ?>
<option value="<?= $breakfast['id']; ?>"><?= $breakfast['name']; ?></option>
<?php endforeach;} ?>
</select>
答案 0 :(得分:0)
当然,您可以使用要显示的selected
上的<option>
属性。
首先从第一个selected
:
<option>
属性
<option value="" disabled>Select your breakast</option>
然后在foreach
循环中,您需要if
条件:
$results123 = $optionquery1->fetch();
$selectedrecipe = $results123['id'];
foreach($breakfast as $breakfast): ?>
<option value="<?= $breakfast['id']; ?>" <?php if ($breakfast['id'] == $selectedrecipe) echo 'selected' ?>><?= $breakfast['name']; ?></option>
<?php endforeach;} ?>
注意我已经删除了while
循环,因为我们确定Mysql只返回一条记录(WHERE ID = <ID>
)。