在PHP页面生成一个2015-2030年的日历,要求突出当前日期,并可以通过点击←→按钮更改月份。并通过连接数据库,把数据表ot_apply中所有employee_id和reason列都以超链接的形式输出到对应日期的单元格里。点击某条链接时会弹出窗口显示该起始日期和时间详情。请生成完整代码
时间: 2024-02-09 13:11:38 浏览: 92
以下是一个简单的实现,代码注释中有详细说明:
```php
<?php
// 连接数据库
$servername = "localhost";
$username = "yourusername";
$password = "yourpassword";
$dbname = "yourdbname";
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 获取当前月份和年份
$month = date("n");
$year = date("Y");
// 如果有通过POST请求传过来的月份和年份,就使用它们
if (isset($_POST["month"]) && isset($_POST["year"])) {
$month = $_POST["month"];
$year = $_POST["year"];
}
// 计算当前月份的天数
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $month, $year);
// 获取当前日期
$current_day = date("j");
// 根据月份和年份查询数据库中的数据
$sql = "SELECT employee_id, reason, start_date, end_date FROM ot_apply WHERE MONTH(start_date) = $month AND YEAR(start_date) = $year";
$result = $conn->query($sql);
// 创建一个二维数组用于存储每一天的数据
$data = array();
for ($i = 1; $i <= $days_in_month; $i++) {
$data[$i] = array();
}
// 把数据库中查询到的数据按日期存入二维数组中
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$start_date = strtotime($row["start_date"]);
$end_date = strtotime($row["end_date"]);
for ($i = $start_date; $i <= $end_date; $i += 86400) {
$day = date("j", $i);
$data[$day][] = array("employee_id" => $row["employee_id"], "reason" => $row["reason"], "start_date" => $row["start_date"], "end_date" => $row["end_date"]);
}
}
}
// 输出日历表格
echo "<table>";
echo "<tr><th colspan='7'>" . date("F Y", strtotime($year . "-" . $month . "-01")) . "</th></tr>";
echo "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>";
echo "<tr>";
$weekday = date("w", strtotime($year . "-" . $month . "-01"));
for ($i = 0; $i < $weekday; $i++) {
echo "<td></td>";
}
for ($i = 1; $i <= $days_in_month; $i++) {
if ($i == $current_day) {
echo "<td style='background-color: yellow;'>";
} else {
echo "<td>";
}
echo "<a href='#' onclick='showDetails(\"" . json_encode($data[$i]) . "\")'>$i</a>";
echo "</td>";
$weekday++;
if ($weekday == 7) {
echo "</tr><tr>";
$weekday = 0;
}
}
for ($i = $weekday; $i < 7; $i++) {
echo "<td></td>";
}
echo "</tr>";
echo "</table>";
// 输出JavaScript代码,用于显示弹出窗口和显示数据详情
echo "<script>";
echo "function showDetails(data) {";
echo " var details = '';";
echo " data = JSON.parse(data);";
echo " for (var i = 0; i < data.length; i++) {";
echo " var start_date = new Date(data[i].start_date);";
echo " var end_date = new Date(data[i].end_date);";
echo " details += '<p>Employee ID: ' + data[i].employee_id + '</p>';";
echo " details += '<p>Reason: ' + data[i].reason + '</p>';";
echo " details += '<p>Start Date: ' + start_date.toDateString() + '</p>';";
echo " details += '<p>End Date: ' + end_date.toDateString() + '</p><hr>';";
echo " }";
echo " var popup = window.open('', 'Details', 'width=400,height=400');";
echo " popup.document.write(details);";
echo "}";
echo "</script>";
// 输出前进和后退按钮,用于更改月份
echo "<form action='' method='post'>";
echo "<input type='hidden' name='month' value='" . (($month == 1) ? 12 : ($month - 1)) . "'>";
echo "<input type='hidden' name='year' value='" . (($month == 1) ? ($year - 1) : $year) . "'>";
echo "<input type='submit' value='←'>";
echo "</form>";
echo "<form action='' method='post'>";
echo "<input type='hidden' name='month' value='" . (($month == 12) ? 1 : ($month + 1)) . "'>";
echo "<input type='hidden' name='year' value='" . (($month == 12) ? ($year + 1) : $year) . "'>";
echo "<input type='submit' value='→'>";
echo "</form>";
// 关闭数据库连接
$conn->close();
?>
```
注意:
1. 这个实现中没有考虑安全性和容错性,仅供参考。
2. 这个实现中使用了一个JavaScript函数`showDetails`来显示弹出窗口和显示数据详情,它接受一个JSON格式的字符串作为参数。具体实现方法是,将二维数组`data`转换为JSON格式的字符串,然后通过JavaScript的`JSON.parse`函数将其转换为JavaScript对象,然后遍历这个对象,生成HTML代码,并将其写入弹出窗口的文档中。
3. 为了使代码更简洁,这个实现中没有使用任何框架或库,但这并不意味着你不能使用它们。实际上,使用一些流行的前端框架,如React或Vue.js,可以使代码更容易组织和维护。
阅读全文