<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>";
?>
The problem is when I enter more than one form, only the latest task is displayed. I’m not sure if I have to store it in a database or if there’s another way of doing it.
I tried to make it so that if the task name is different than the first one, I added a new array to the $tasks but it still only displayed the 2nd task as the variable $task_name is still the same for both.
New contributor
buffalo09 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.