gpt4 book ai didi

php - 基于日期时间mysql创建html select

转载 作者:行者123 更新时间:2023-11-29 03:22:40 24 4
gpt4 key购买 nike

所以,我有这张表,其中的数据与显示的类似:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ id(int) + stamp(datetime) + initials(text) + rating(decimal[4,2]) +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ + + + +
+ 1 + 2016-12-21 12:23:01 + STA + 1.07 +
+ 2 + 2016-11-17 16:15:19 + CKO + 8.22 +
+ 3 + 2016-08-06 12:33:00 + VER + 4.58 +
+ 4 + 2016-08-14 19:54:34 + FLO + 2.43 +
+ 5 + 2016-12-11 01:09:22 + WIS + 9.16 +
+ 6 + 2016-09-28 05:43:51 + WIN + 5.53 +
+ ... + ... + ... + ... +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

我想使用月/年组合创建一个选择,例如:

<select name="display">
<option value="08-16">08-16</option>
<option value="09-16">09-16</option>
<option value="11-16">11-16</option>
<option value="12-16">12-16</option>
</select>

在后端

<?php
$myoptions = "<select name=\"display\">\n";
$myquery = "SELECT QUERY HERE";
$myresult = mysqli_query($mysqli, $myquery) or die(mysqli_error($mysqli));
while($myrow(mysqli_fetch_array($myresult)) {
$thedateandtime = explode(" ", $myrow['stamp']);
$dateparts = explode("-", $thedateandtime[0]);
$year = $dateparts[0];
$month = $dateparts[1];
$myoptions .= "<option value=\"".$month."-".$year."\">".$month."-".$year."</option>\n";
}
$myoptions .= "</select>\n";
echo $myoptions;
?>

我不想以多个月/年组合结束,比如在选择选项中有两个十二月和两个八月。想法?

最佳答案

好的,所以我的新手(在 StackOverflow 上)自己找到了答案,所以在这里以防其他人遇到问题:

$myoptions = "<select name=\"display\">\n";
$myquery = "SELECT CONCAT( MONTH(`stamp`), '-', CONCAT( YEAR(`stamp`) ) ) as `mystamp` FROM `entries` GROUP BY YEAR(`stamp`), MONTH(`stamp`)";
$myresult = mysqli_query($mysqli, $myquery) or die(mysqli_error($mysqli));
while($myrow = mysqli_fetch_array($myresult)) {
$myoptions .= "<option value=\"".$myrow['mystamp']."\">".$myrow['mystamp']."</option>\n";
}
$myoptions .= "</select>\n";
echo $myoptions;

这是可能的,因为“stamp”列是一个日期时间字段。

关于php - 基于日期时间mysql创建html select,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41274416/

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