0
Follow
0
View

Filter the data records by time and display them to the page table

dokejia 注册会员
2023-02-27 11:36

This answer quotes ChatGPT

You can filter records by month by adding a date selector to the read records page. After the user selects a date, the date is passed to a background PHP file, where SQL query statements are used to filter out records that fit the date range and display them in the table.

Here is a simple example code:

HTML page:



html>
<html>
<head>
    <title>账目查询title>
head>
<body>
    <form action="get_records.php" method="post">
        <label for="month">选择月份:label>
        <input type="month" id="month" name="month">
        <input type="submit" value="查询">
    form>

    <table>
        <thead>
            <tr>
                <th>日期th>
                <th>金额th>
                <th>备注th>
            tr>
        thead>
        <tbody>
            
                // PHP 代码用于从数据库中读取记录并显示在表格中
                // 代码略
            ?>
        tbody>
    table>
body>
html>


PHP file get_records.php:



// 连接到数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}

// 获取用户选择的月份
$month = $_POST["month"];

// 使用 SQL 查询语句从数据库中获取符合该月份的记录
$sql = "SELECT * FROM account WHERE MONTH(FROM_UNIXTIME(timestamp)) = MONTH('$month')";
$result = $conn->query($sql);

// 显示记录在表格中
if ($result->num_rows > 0) {
    // 输出每行数据
    while($row = $result->fetch_assoc()) {
        echo "" . $row["timestamp"] . "" . $row["amount"] . "" . $row["remark"] . "";
    }
} else {
    echo "0 结果";
}

$conn->close();
?>

In the above example, the get_records.php file gets the MONTH selected by the user, then uses the month function to select the records that match that month from the database and output them to the table. If the user does not select a date, all records are displayed by default.

About the Author

Question Info

Publish Time
2023-02-27 11:36
Update Time
2023-02-27 11:36