gpt4 book ai didi

PHP MYSQL - 如何创建包含按天/周分组的多个下拉列表的表单

转载 作者:搜寻专家 更新时间:2023-10-30 23:38:27 24 4
gpt4 key购买 nike

我有一个名为“选择”的表,结构如下....

-----------------------------------------------
id | serving_date | serving_description
-----------------------------------------------
1 | 04/07/2016 | Food description goes here
2 | 04/07/2016 | Food description goes here
3 | 04/07/2016 | Food description goes here
4 | 05/07/2016 | Food description goes here
5 | 05/07/2016 | Food description goes here
6 | 05/07/2016 | Food description goes here
7 | 06/07/2016 | Food description goes here
8 | 06/07/2016 | Food description goes here
9 | 06/07/2016 | Food description goes here
11 | 07/07/2016 | Food description goes here
12 | 07/07/2016 | Food description goes here
13 | 07/07/2016 | Food description goes here
-----------------------------------------------

我现在需要用 PHP 生成一个订单,我想要的是每个日期的下拉框,所以使用上面的示例表,我希望每天有 4 个下拉框,其中包含 3 个选项当天可用。

我很擅长编写下拉框代码以从另一个表中读取数据,但完全无法理解如何遍历每一行并将行按日期自动分组到单独的下拉列表中。

非常感谢您提供的任何帮助。

更新...

我已经设法使用以下方法让它工作......

$current_date = null;
$current_week = null;

while ($row = $stmt->fetch()) {

if ($row["choice_date"] != $current_date) {
$current_date = $row["choice_date"];

echo "<select name=" . $current_date . "";
echo " . $current_date . ";
}
echo '<option value="'.$row['choice_description'].'">'.$row['choice_description'].'</option>';
}

下一步是提交表单进行处理,但我很挣扎......通常我会将 $_GET 与表单中的字段名称一起使用。但在这种情况下,名称被设置为值 $current_date 并且随着脚本的运行而改变。

有没有办法简单地捕获用户所做的所有选择?

我还附上了数据库的屏幕截图,以使事情更清楚一些。 choices_table

最佳答案

首先从数据库中获取数据

function getResults($conn){
$sql = "SELECT * FROM choices";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$dataArray=array(); // Create a blank array
while($row = $result->fetch_assoc()) {
$dataArray[$row['serving_date']][]=$row;
// Store data in array by date

}
return $dataArray;
}else {
echo "0 results";
}

}


您的数据将进入一个数组,其中键是唯一日期,值是该日期的值,

现在您可以显示一个下拉列表,您可以在其中再次遍历日期数组并单击下拉列表的任何选项,只需对该日期的 array_key_exists() 进行条件检查,如果存在则获取这样的数据

print_r($dataArray['Selected Date from Dropdown']);

我希望你明白这背后的逻辑

关于PHP MYSQL - 如何创建包含按天/周分组的多个下拉列表的表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38369916/

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