<h1>Calendar and Task Creator</h1>
<div class="navigation-text">Click the arrows to switch between months</div>
<div class="navigation-text">Click the dates to create tasks</div>
<?php
$month = isset($_GET['month']) ? $_GET['month'] : date('n');
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
// Links
$prevMonth = ($month == 1) ? 12 : $month - 1;
$prevYear = ($month == 1) ? $year - 1 : $year;
$nextMonth = ($month == 12) ? 1 : $month + 1;
$nextYear = ($month == 12) ? $year + 1 : $year;
echo "<div class='navigation'>";
echo "<a href='index.php?month=$prevMonth&year=$prevYear'><</a>";
echo "<span> | </span>";
echo "<a href='index.php?month=$nextMonth&year=$nextYear'>></a>";
echo "</div>";
// number of days
$numDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$monthName = date('F', $firstDay);
//day of the week for the first day of the month
$dayOfWeek = date('D', $firstDay);
$daysOfWeek = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
$firstDayIndex = array_search($dayOfWeek, $daysOfWeek);
// Display
echo "<h2>$monthName $year</h2>";
echo "<table>";
echo "<tr>";
foreach ($daysOfWeek as $day) {
echo "<th>$day</th>";
}
echo "</tr>";
echo "<tr>";
$dayCount = 1;
for ($i = 0; $i < $firstDayIndex; $i++) {
echo "<td></td>";
}
while ($dayCount <= $numDays) {
for ($i = $firstDayIndex; $i < 7; $i++) {
if ($dayCount <= $numDays) {
echo "<td><a href='form.php?day=$dayCount&month=$month&year=$year'>$dayCount</a></td>";
$dayCount++;
} else {
echo "<td></td>";
}
}
echo "</tr>";
if ($dayCount <= $numDays) {
echo "<tr>";
}
$firstDayIndex = 0;
}
echo "</table>";
// ** Here is where I'm unsure what to do **
// printing tasks
$task_name=$_GET['task_name'];
$day=$_GET['day'];
$month=$_GET['month'];
$year=$_GET['year'];
$tasks = array(
array(
'name' => '$task_name',
'day' => $day,
'month' => $month,
'year' => $year
),
);
// Display
echo "<h2>Tasks</h2>";
echo "<table>";
echo "<tr><th>Task Name</th><th>Date</th></tr>";
foreach ($tasks as $task) {
echo "<tr>";
echo "<td>{$task_name}</td>";
echo "<td>{$task['day']}/{$task['month']}/{$task['year']}</td>";
echo "</tr>";
}
echo "</table>";
?>
问题是当我输入多个表单时,只显示最新的任务。我不确定是否必须将其存储在数据库中,或者是否有其他方法可以做到这一点。
我尝试这样做,如果任务名称与第一个任务名称不同,我会向 $tasks 添加一个新数组,但它仍然只显示第二个任务,因为变量 $task_name 对于两个任务仍然相同。
对于这种情况,理想情况下,您应该使用某种数据库来存储数据或在每次操作后保留数据。
如果您暂时不喜欢将数据存储在数据库中,最好是将数据存储在会话、cookie 或本地存储等客户端存储设施中。
参考下面的代码使用session来实现你所需要的:
<?php
session_start(); // Start or resume a session
$month = isset($_GET['month']) ? $_GET['month'] : date('n');
$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
$prevMonth = ($month == 1) ? 12 : $month - 1;
$prevYear = ($month == 1) ? $year - 1 : $year;
$nextMonth = ($month == 12) ? 1 : $month + 1;
$nextYear = ($month == 12) ? $year + 1 : $year;
echo "<div class='navigation'>";
echo "<a href='index.php?month=$prevMonth&year=$prevYear'><</a>";
echo "<span> | </span>";
echo "<a href='index.php?month=$nextMonth&year=$nextYear'>></a>";
echo "</div>";
$numDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$monthName = date('F', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysOfWeek = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
$firstDayIndex = array_search($dayOfWeek, $daysOfWeek);
echo "<h2>$monthName $year</h2>";
echo "<table>";
echo "<tr>";
foreach ($daysOfWeek as $day) {
echo "<th>$day</th>";
}
echo "</tr>";
echo "<tr>";
$dayCount = 1;
for ($i = 0; $i < $firstDayIndex; $i++) {
echo "<td></td>";
}
while ($dayCount <= $numDays) {
for ($i = $firstDayIndex; $i < 7; $i++) {
if ($dayCount <= $numDays) {
echo "<td><a href='form.php?day=$dayCount&month=$month&year=$year'>$dayCount</a></td>";
$dayCount++;
} else {
echo "<td></td>";
}
}
echo "</tr>";
if ($dayCount <= $numDays) {
echo "<tr>";
}
$firstDayIndex = 0;
}
echo "</table>";
if (isset($_GET['task_name'], $_GET['day'], $_GET['month'], $_GET['year'])) {
$task_name = $_GET['task_name'];
$day = $_GET['day'];
$month = $_GET['month'];
$year = $_GET['year'];
$_SESSION['tasks'][] = array(
'name' => $task_name,
'day' => $day,
'month' => $month,
'year' => $year
);
}
echo "<h2>Tasks</h2>";
echo "<table>";
echo "<tr><th>Task Name</th><th>Date</th></tr>";
if (isset($_SESSION['tasks'])) {
foreach ($_SESSION['tasks'] as $task) {
echo "<tr>";
echo "<td>{$task['name']}</td>";
echo "<td>{$task['day']}/{$task['month']}/{$task['year']}</td>";
echo "</tr>";
}
}
echo "</table>";
?>