These are the rows: This is my picture of whats in the rows:对于一个项目,我必须制作一个日历。日期和月份必须按时间顺序排列,如名称:(DD-MM-YYYY)。现在我已经用html和php制作了表格,但我不知道如何对它进行排序。
<?php
$connection = new mysqli('localhost','root','','calendar');
$sql = "SELECT * FROM birthdays";
$result = $connection->query($sql);
$birthdayList = $result->fetch_all(MYSQLI_ASSOC);
?>
<!doctype html>
<html>
<head>
<title>Verjaardagskalender</title>
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>
<table>
<?php
foreach($birthdayList as $birthdays){
?>
<tr>
<td><?php echo $birthdays['person'];?></td>
<td><?php echo $birthdays['day'];?></td>
<td><?php echo $birthdays['month'];?></td>
<td><?php echo $birthdays['year'];?></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
答案 0 :(得分:0)
$sql = "SELECT * FROM birthdays ORDER BY STR_TO_DATE(CONCAT(year, '-', CONCAT(month, '-', date)), '%Y-%m-%d') ASC";
更好的方法是将组合列用于日期 - YYYY-MM-DD
,而不是分别使用3列。为此,请执行以下操作:
在mysql数据库中,创建一个DATETIME类型的列。将完整日期存储为YYYY-MM-DD
答案 1 :(得分:0)
我认为最好的方法是在SQL中对数据进行排序。我的意思是你可以在这种情况下使用SQL命令。
您可以使用ORDER BY;
SELECT * from tableName ORDER BY fieldName ASC / DESC
(在您的情况下,您应该使用ASC(升序))
<?php
$connection = new mysqli('localhost','root','','calendar');
$sql = "SELECT * FROM birthdays ORDER BY birthday_field ASC";
$result = $connection->query($sql);
$birthdayList = $result->fetch_all(MYSQLI_ASSOC);
?>
所以你确实订购了它们:)
此致
答案 2 :(得分:0)
我认为你应该用一个字段来保持生日。
不是日,月,年。你把所有的东西都像1987-12-17一样放在桌子里
您的字段类型应为DATE;
birthday =&gt; DATE
然后您可以使用SQL
订购它们SELECT * from birtdays ORDER BY birthday ASC
好吧,如果你得到每一天,每月,每年;你有两个选择;
使用PHP;
<?php
// 1987-12-17
$birth = explode("-",$birthdays['birthday']);
$day = $birth[2];
$month = $birth[1];
$year = $birth[0];
?>
<td><?php echo $day;?></td>
<td><?php echo $month ;?></td>
<td><?php echo $year ;?></td>
使用SQL;
SELECT DAY(birthday) as day, MONTH(birthday) as month, YEAR(birthday) as birthday from birthdays ORDER BY birthday ASC
再次,您可以使用php获取它们;
<td><?php echo $birthdays['day'];?></td>
<td><?php echo $birthdays['month'];?></td>
<td><?php echo $birthdays['year'];?></td>
答案 3 :(得分:0)
我阅读了您的图像并模拟了一个小提琴演示:https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=369bb7f53c2e5f190d5727c6453c892e
我没有费心去匹配您的列类型,但是对于我显示查询的结果集而言,它们没有任何实际意义。实际上,您实际上不应该将年份值存储为浮点数,而只是在显示给最终用户之前增加了更多准备工作。
以下查询将仅选择您要使用的列,并按月(升序)和第二天(升序)对结果集进行排序。年份值未计入。
SELECT `person`, `day`, `month`, `year` FROM `birthdays` ORDER BY `month`, `day`
代码:
<!doctype html>
<html>
<head>
<title>Verjaardagskalender</title>
<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
if (!$conn = new mysqli("localhost", "root","","calendar")) {
echo "Database Connection Error"; // don't show this publicly: $conn->connect_error;
} elseif (!$result = $conn->query("SELECT `person`, `day`, `month`, `year` FROM `birthdays` ORDER BY `month`, `day`")) {
echo "Syntax Error"; // don't show this publicly: $conn->error;
} elseif (!$result->num_rows) {
echo "Possible Logic Error: No Rows";
} else {
echo "<table>";
echo "<tr><th>Persoon</th><th>Dag</th><th>Maand</th><th>Jaar</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>{$row['person']}</td>";
echo "<td>{$row['day']}</td>";
echo "<td>{$row['month']}</td>";
echo "<td>{$row['year']}</td>";
echo "</tr>";
}
echo "</table>";
}
?>
</body>
</html>