我正在尝试创建一个非常简单的时间跟踪应用程序,在数据库中只有3个值: description , time-spent 和 date 和我需要按日期对结果进行分组并生成HTML表格。
例如:
今天(10.07)这个日期我在数据库中有3条记录,我需要为这个日期生成HTML表格。
Tommorow(11.07)当我使用新日期创建新记录时 - 它会创建新的HTML表格。
在那里,我有很多具有特定日期的HTML表格。
(HTML表格为自己的日期)。
那么如何按日期分组以及如何生成HTML表格呢?
下面的屏幕截图反映了我想要实现的目标:
以下是我的代码:
function getLogs( $con ) {
$stmt = $con->prepare( "SELECT * FROM timelogsapp GROUP BY DATE ORDER BY date DESC" );
$stmt->execute();
$result = $stmt->fetchAll();
foreach( $result as $row ) {
echo '<table class="list-table">';
echo '<thead>';
echo '<span class="date">';
echo datetime();
echo '</span>';
echo '<tr>';
echo '<th>Description</th>';
echo '<th>Time</th>';
echo '<th>Date</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>' . $row['description'] . '</td>';
echo '<td>' . $row['time'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
}
答案 0 :(得分:0)
您可以创建SQL WHERE
日期在所需范围内。所以你说你想成为今天,它看起来像这样:
这应创建多个查询,您可以向WHERE子句添加更多查询,以获取自定义日期,我希望这可以让您了解可以使用的内容。
每条记录都会创建它的唯一表格,因为它们位于foreach
子句中。
<?php
$today = date('d.m');
$today_query = "SELECT * FROM timelogsapp WHERE date = $today DESC";
$yesterday = date('d.m', time() - 60 * 60 * 24);
$yesterday_query = "SELECT * FROM timelogsapp WHERE date = $yesterday DESC";
$custom = "";
if (isset($_POST['date']))
{
$custom = $_POST['date'];
}
$custom_query = "SELECT * FROM timelogsapp WHERE date = $custom DESC";
$tom = new DateTime('tomorrow');
$tomorrow = $tom->format('d.m');
$tomorrow_query = "SELECT * FROM timelogsapp WHERE date = $tomorrow DESC";
function getLogs($con){
$stmt = $con->prepare("SELECT * FROM timelogsapp WHERE date = $today AND date = $yesterday AND date = $custom DESC");
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row) {
echo '<table class="list-table">';
echo '<thead>';
echo '<span class="date">';
echo datetime();
echo '</span>';
echo '<tr>';
echo '<th>Description</th>';
echo '<th>Time</th>';
echo '<th>Date</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
echo '<tr>';
echo '<td>'.$row['description'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
}
}
?>
当然,通过使用一些PHP,你可以使动态变得更加动态。但是,这将从timelogsapp
中选择日期等于10.07的所有记录。然后按降序显示。
答案 1 :(得分:0)
我设法达到了我一直在寻找的结果。
// getting all dates from database with limit
$all_dates_query = "SELECT * FROM timelogsapp GROUP BY date(date) ORDER BY date DESC LIMIT $limit OFFSET $offset";
$stmt = $con->prepare($all_dates_query);
$stmt->execute();
$all_dates = $stmt->fetchAll();
function getLogs($con, $all_dates)
{
foreach ($all_dates as $row) {
$this_date = $row['date'];
$all_dates = "SELECT * FROM timelogsapp WHERE date(date) = date('$this_date') ORDER BY date DESC"; //getting all logs for current date
$stmt = $con->prepare($all_dates);
$stmt->execute();
$result = $stmt->fetchAll();
//generating html table with logs
echo '<table class="list-table">';
echo '<thead>';
echo '<span class="date">';
echo datetime($this_date);
echo '</span>';
echo '<tr>';
echo '<th>Description</th>';
echo '<th>Time</th>';
echo '<th>Date</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($result as $row) {
echo '<tr>';
echo '<td>'.$row['description'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td>'.date('d.m.Y H:i', strtotime($row['date'])).'</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
}
}