Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Trouble with a Calendar Script

Posted by Chris_Isaac (Chris Isaac), 28 February 2007

I'm having a bit of trouble getting a calendar script to work.  I can get it to print out correctky, but what I would like is to tie it to a musql database, so if for example there is an event in the database for 1st april 2007, the 1st would be highlighted as a link, but if no events are on no link would be there.

So far the calendar is working on its own, also I can get the information out of teh database, but can't seem to do both at the same time

Here is the calendar script:

$PageTitle = "Calendar";

require ("header.php");
// If the $Month and $Year values don't exist, make them the current month and year.
if ((!$Month) && (!$Year)) {
     $Month = date ("m");
     $Year = date ("Y");
//  Calculate the viewed Month.
$Timestamp = mktime (0, 0, 0, $Month, 1, $Year);
$MonthName  =  date("F", $Timestamp);

//  Make a table with the proper month.

<TR><TD COLSPAN=7 ALIGN=CENTER><FONT COLOR=blue><?php print"$MonthName $Year"; ?></TD></TR>


$MonthStart = date("w", $Timestamp);
if ($MonthStart == 0) {
     $MonthStart = 7;
$LastDay = date("d", mktime (0, 0, 0, $Month+1, 0, $Year));
$StartDate = -$MonthStart;

#this just proves info is present in the database
$conn= mysql_connect("localhost","username","password") or die("connection problem");
$db= mysql_select_db("calendar",$conn) or die("database problem");
$sql="select * from cal";
$ex=mysql_query($sql, $conn);
     echo"$row[id], $row[date] $row[event] <br>";

for ($k = 1; $k <= 6; $k++) { // Print 6 rows.
     print ("<TR BGCOLOR=WHITE>");
     for ($i = 1; $i <= 7; $i++) { // Use 7 columns

           if (($StartDate <= 0) || ($StartDate > $LastDay)) {
                 print ("<TD BGCOLOR=white>&nbsp</TD>");
           } elseif (($StartDate >= 1) && ($StartDate <= $LastDay)) {
#this is where I need another if statement, but not sure what!!
                 print ("<TD ALIGN=CENTER>: $StartDate :</TD>");
                 else {
                 print ("<TD ALIGN=CENTER>$StartDate</TD>");

     print ("</TR>\n");
print ("</TABLE>\n");

// Make the form.
print ("<FORM ACTION=\"calendar.php\" METHOD=GET>\n");
print ("Select a new month to view:\n");
print ("<SELECT NAME=Year><OPTION VALUE=2007>2007</OPTION>\n<OPTION VALUE=2008>2008</OPTION>\n<OPTION VALUE=2009>2009</OPTION>\n<OPTION VALUE=2010>2010</OPTION>\n<OPTION VALUE=2011>2011</OPTION>\n</SELECT>\n");
print ("<INPUT TYPE=SUBMIT NAME=SUBMIT VALUE=\"Submit!\">\n");
print ("</FORM>\n");

require ("footer.php");

The mysql database/table has 3 fields, ID, Date and event,

I've put where I think I need another IF statement, what I don;t know is how to compare the date from the calendar to the date in the datebase , any pointers gratefully received?



Posted by admin (Graham Ellis), 28 February 2007
Chris, I'm feeling pretty tired at the moment and that's a lot of code for a Wednesday afternoon.  


I've been asked (as it happens) to work on a calendar example with my class tomorrow - not your code directly but I SHALL be coming up with a calendar handling example that has links on som edays but not on others - worked on one recently that related to hotel availability, funnily enough!  I'll come back and post a follow up

Posted by Chris_Isaac (Chris Isaac), 28 February 2007
That would be great, thank you.

Posted by admin (Graham Ellis), 1 March 2007
What I desribed as my "hot diary" script is here ... and you can run it from there too.  Looking at it v your question I'm not really sure it will help much - although I do have a modified varient that passes a parameter into each day cell and colours it depending on room availablility. The code for THAT is - err - contorted and something I'm not proud of.

Posted by Chris_Isaac (Chris Isaac), 6 March 2007
Hi Graham,

Firstly thanks for posting this and sorry for the delay in responding, been up to my neck in it last few days.

Haven't had a good chance to have a play with this script, but hopefully (once kids are in bed) will do tonight

I think I've narrowed down where I'm stuck, in the loop :

for ($k = 1; $k <= 6; $k++) { // Print 6 rows.
 print ("<TR BGCOLOR=WHITE>");
 for ($i = 1; $i <= 7; $i++) { // Use 7 columns

  if (($StartDate <= 0) || ($StartDate > $LastDay)) {
   print ("<TD BGCOLOR=white>&nbsp</TD>");  
  } elseif (($StartDate >= 1) && ($StartDate <= $LastDay)) {
#this is where I need another if statement, but not sure what!!
   print ("<TD ALIGN=CENTER>: $StartDate :</TD>");  
  else {
   print ("<TD ALIGN=CENTER>$StartDate</TD>");  


What I think I have to do is for each date look and see if there is an equivelent date stored in the mysql database, if so make that number a hyperlink to execute the query of bring up the event details for that date.

Will let you know how I get on.

Thanks again.

Posted by admin (Graham Ellis), 7 March 2007
Like you, Chris, rushed off my tootsie!   I note you'll report back ... yes, please do.  I have a Perl course tomorrow, sandwiched between Dilton Marsh Halt at 06:27 and a phone call from a top civil servant in the evening.  A long story ...

Posted by bschultz (bschultz), 19 March 2007
I have a (somewhat) related question.  I have a form that needs a dropbox showing the next 7 days, converted from this format: "2007-03-19" to this format: "Monday, March 19".

That didn't make much sense, did it?  Let's try again.

The dropbox has Monday, March 19, but when it writes the value to the form field it would be 2007-03-19.

Drop Box                          Value Written
Monday, March 19            2007-03-19
Tuesday, March 20           2007-03-20
Wednesday, March 21     2007-03-21
Thursday, March 22         2007-03-22
Friday, March 23              2007-03-23
Saturday, March 24         2007-03-24
Sunday, March 25            2007-03-25

Two that possible, and if so, can someone steer me in the right direction?



Posted by admin (Graham Ellis), 19 March 2007
Try this ...

If it's right, here's the source:
function daysahead($ndays) {
  $now = time();
  for ($step = 0; $step < $ndays; $step ++) {
       $report = $now + $step * 24 * 3600;
       $form1 = date("Y-m-d",$report);
       $form2 = date("j F",$report);
       $selbox .= "<option value=$form1>$form2</option>";
  return "<select name=whichday>$selbox</select>";

<title>Selection box - the week ahead</title>
<h1>The week ahead</h1>
<?php if ($_REQUEST[whichday]) {
       print ("previous selection was $_REQUEST[whichday]");
} else {
       print ("Results will come here");
<br /><form>
<?php print(daysahead(14)); ?>
<br /><input type=submit></form><br />
Please choose the day.

Posted by bschultz (bschultz), 19 March 2007
Thanks, Graham...that was it!

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: • WEB: • SKYPE: wellho