Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have built a Calendar using SQL and PHP, at the moment the calendar displays one week at a time, I am wondering how to display a full month, rather than one week?

What I have tried:

<!doctype HTML>
<html>
	<head>
		<link rel="stylesheet" href="calendar.css">
	</head>
	<body>
		<section class="calendar">
			<section class="modifyEvent">
			<h2><a href="editCalendar.php">Add Event</a></h2>
		</section>
			<?php
			include("dbConnect.php");
				// variables to hadle  retreiving information about the month and the days within
				$day = date('j');
				$daysInMonth = date('t');
				$dayOfWeek = date('N');
				$curentMonth = date('m');
				$currentYear = date('Y');
				// variables to handle looping of calendar table
				$displayedDays = 7;
				$dateCount = 1;
				echo '<table>';
				// loop repeats the creation of cels where one cell per day for the table header loops as many times as stated in displayedDays variable
				do{
					// switch to change the day of week value from being a number to an english representation
					switch ($dayOfWeek) {
						case 1:
							$textDayOfWeek = 'Monday';
							break;
						case 2:
							$textDayOfWeek = 'Tuesday';
							break;
						case 3:
							$textDayOfWeek = 'Wendnesday';
							break;
						case 4:
							$textDayOfWeek = 'Thursday';
							break;
						case 5:
							$textDayOfWeek = 'Friday';
							break;
						case 6:
							$textDayOfWeek = 'Saturday';
							break;
						case 7:
							$textDayOfWeek = 'Sunday';
							break;
						
						default:
							echo "something is wrong";
							break;
					}
					// creates the table header
					echo '<th>'. $textDayOfWeek .'</th>';
					$dayOfWeek = $dayOfWeek +1;
					$dateCount = $dateCount +1;
					// resets the count to the start of the week so week value repeats from monday
					if ($dayOfWeek > 7) {
						$dayOfWeek = 1;
					}
				}while ( $dateCount <= $displayedDays);
				// reset dateCount variable to zero so it can be used for the day cell
				$dateCount = 1;
				// starts table row outside of loop so it does not get repeated
				echo '<tr>';
					do{
						// queries to get information for the constructed day
						$sql = 'SELECT title, description FROM calendar WHERE eventDay ='. $day;
						//echo $day;
						$result = $conn->query($sql);
						// checks for errors in sql query
						if (!$result){
							echo "error";
							print_r($result->errInfo());
						}
						// set variables that pick up information from server to be null
						$eventTitle = null;
						$eventDescription = null;
						// assaigns retreived information to variables if information is present
						echo '<td><h3>'. $day. '</h3><div class="events">';
						foreach ($result as $row) {
							echo '<h4>'. $row['title']. '</h4></br><p>'. $row['description'] .'</p></br>';
						}
						echo "</div></td>";
						// Increment day to diplay new value in each cell
						$day = $day + 1;
						// handles end of month to reset to first of month by changing the current month and the day order
						if($day > $daysInMonth){
							$day = 1;
							$curentMonth = $curentMonth + 1;
							if($currentMonth > 12){
								$currentMonth = 1;
								$currentYear = $currentYear + 1;
							}
						}
						// Increments date count so it stops creating table rows after it reaches the number of desired days displayed
						$dateCount = $dateCount +1;
					}while($dateCount <= $displayedDays);
				// ends table row
				echo '</tr>';
				// ends calendar table
				echo '</table>';
				// disconect from database
				$conn = null;
			?>
	</section>
	</body>
</html>
Posted
Updated 22-Mar-18 2:28am
Comments
Jochen Arndt 22-Mar-18 7:09am    
Where is the problem?
Define your layout (there are multiple designs, just have a look at some existing calendars) and write the code for the table and the cell content.

1 solution

Use SQL to build a dataset of days for the designated month . I would put that sql code into a stored procedure so that it can be used over and over without having to be tweaked for the current month/year. Here's the SQL you need to do that. Just give it a date in the month/year in which you are interested, and it will return the info you want for the entire month. Once you get this data, you can simply loop through the returned data set and populate your calendar.

SQL
-- when you put this into a stored proc, you would pass a date (probably the first of 
-- the desired month, but it doesn't have to be), and stored proc would create the  
-- following dates based on the date supplied. In the example below, I used GETDATE() 
-- with returns the current date.
declare	@startDate DATE = DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0);
declare @endDate   DATE = DATEADD(DAY,-1,DATEADD(month, DATEDIFF(month, 0, @startDate)+1, 0))   -- the end of the date range

select @startDate, @endDate

SET NOCOUNT ON;
-- sanity checks
if (@endDate = '1900-1-1')
BEGIN
	SET @endDate = CAST(GETDATE() AS DATE);
END
if (@startDate = '1900-1-1')
BEGIN
	SET @startDate = DATEADD(YEAR, -5, @endDate);
END
IF (@startDate > @endDate)
BEGIN
	DECLARE @temp date = @startDate;
	SET @startDate = @endDate;
	SET @endDate = @temp;
END

-- Get calendar data for every day in the date range . We should have one day for every 
-- day between and including the specified date range dates.     
;WITH n AS 
(
    SELECT TOP (DATEDIFF(DAY, @startDate, @endDate) + 1) n = ROW_NUMBER() OVER (ORDER BY [object_id]) 
    FROM sys.all_objects
)
SELECT DATEADD(DAY, n-1, @startDate) AS CalendarDate
       ,DATEPART(YEAR, DATEADD(DAY, n-1, @startDate)) AS CalendarYear
       ,DATEPART(MONTH, DATEADD(DAY, n-1, @startDate)) AS CalendarMonth
       ,DATEPART(WEEKDAY, DATEADD(DAY, n-1, @startDate)) AS WeekDayNumber
       ,DATENAME(WEEKDAY, DATEADD(DAY, n-1, @startDate)) AS WeekDayName
       ,DATENAME(MONTH, DATEADD(DAY, n-1, @startDate)) AS [MonthName]
       ,DATEPART(DAY, DATEADD(DAY, n-1, @startDate)) AS [DayOfMonth]
FROM n


This is a modified version of an CP article I wrote last year. I simply removed all the crap you didn't need for this task.

Build a Calendar Without Pre-Existing Tables[^]
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900