Jump to content

Adding data from database to dynamic calender?


afish674

Recommended Posts

I'm using a calender from CSS tricks: http://css-tricks.com/snippets/php/build-a-calendar-table/#comment-73575 What I want to do is use this as a base and then do some database queries to input data into the relevent cells about events. The data I have in my database includes day and month. I'm just not sure what code should be added and where in order to get the database entries to appear in the right place! Thanks for any help you can give!

Link to comment
Share on other sites

I don't see the actual code for that, but you'll need to find the part where it prints out each day and add your code there to check for events for that day. How you want to do that is up to you, one way would be to pass an array of events to the function that it will loop through for each day to see if there are any events for that day.

Link to comment
Share on other sites

Sorry, the code is:

<?phpfunction build_calendar($month,$year) {$today_date = date("d");$today_date = ltrim($today_date, '0');	 // Create array containing abbreviations of days of week.	 $daysOfWeek = array('S','M','T','W','T','F','S');	 // What is the first day of the month in question?	 $firstDayOfMonth = mktime(0,0,0,$month,1,$year);	 // How many days does this month contain?	 $numberDays = date('t',$firstDayOfMonth);	 // Retrieve some information about the first day of the	 // month in question.	 $dateComponents = getdate($firstDayOfMonth);	 // What is the name of the month in question?	 $monthName = $dateComponents['month'];	 // What is the index value (0-6) of the first day of the	 // month in question.	 $dayOfWeek = $dateComponents['wday'];	 // Create the table tag opener and day headers	 $calendar = "<table class='calendar'>";	 $calendar .= "<caption>$monthName $year</caption>";	 $calendar .= "<tr>";	 // Create the calendar headers	 foreach($daysOfWeek as $day) {		  $calendar .= "<th class='header'>$day</th>";	 }	 // Create the rest of the calendar	 // Initiate the day counter, starting with the 1st.	 $currentDay = 1;	 $calendar .= "</tr><tr>";	 // The variable $dayOfWeek is used to	 // ensure that the calendar	 // display consists of exactly 7 columns.	 if ($dayOfWeek > 0) {		  $calendar .= "<td colspan='$dayOfWeek'> </td>";	 }		 $month = str_pad($month, 2, "0", STR_PAD_LEFT); 	 while ($currentDay <= $numberDays) {		  // Seventh column (Saturday) reached. Start a new row.		  if ($dayOfWeek == 7) {			   $dayOfWeek = 0;			   $calendar .= "</tr><tr>";		  }		  $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);		  $date = "$year-$month-$currentDayRel";      if($currentDayRel == $today_date ){  $calendar .= "<td class='day' id='today_date' rel='$date'><b>$currentDay</b></td>"; }    else { $calendar .= "<td class='day' rel='$date'>$currentDay</td>"; }		  // Increment counters		  $currentDay++;		  $dayOfWeek++;	 }		 // Complete the row of the last week in month, if necessary	 if ($dayOfWeek != 7) {			  $remainingDays = 7 - $dayOfWeek;		  $calendar .= "<td colspan='$remainingDays'> </td>";	 }		 $calendar .= "</tr>";	 $calendar .= "</table>";	 return $calendar;}?>

Would I need to enter code somewhere around here?

		  $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);		  $date = "$year-$month-$currentDayRel";      if($currentDayRel == $today_date ){  $calendar .= "<td class='day' id='today_date' rel='$date'><b>$currentDay</b></td>"; }    else { $calendar .= "<td class='day' rel='$date'>$currentDay</td>"; }

I'm struggling a bit to follow some of the code as I'm quite new to PHP.

Link to comment
Share on other sites

//DATABASE CONNECTIONtry{  $pdo = new PDO("mysql:dbname=birthdays; host=localhost","root","Rockon!");}catch (PDOException $e){  die ("ERROR: Could not connect: " . $e->getMessage());}$getBdayInfo = "SELECT first_name, second_name, day, month, note FROM birthdays WHERE month = '$monthName' AND username = '$_SESSION[user]'";$result = $pdo->query($getBdayInfo);$row = $result->fetchAll();if ($row > 1){foreach($row as $r){  echo "'$r'";  }}//END QUERY AND CONNECTIONunset($pdo);

I think this code should return a list of

  • First Name
  • Second Name
  • Day
  • Month
  • Note

For the logged in user in the current month, I inserted this above the code block I posted above. I don't get any parse errors when opening this page but I don't get a list either. Also this list wouldn't publish the information in the right calender box. I'd need to test the day against the current day and then insert the data in the right box using that somehow. I'm just not exactly sure how.

Link to comment
Share on other sites

Maybe the problem is the if statement checking if $row is greater than 1. $row is not a number. Once you have the list of events, every time you're printing out a day in the calendar you need to loop through the events and check each one to see if it is on the correct day, then you can print the information for that event.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...