Home > Articles > Programming > PHP

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Linking to the Calendar

Now that we've got a little bit of functionality, let's start tying it into the application. So far, the showday.php file just takes an arbitrary day, but ultimately we want to link to it from the calendar. To do that, we need to create links on the calendar itself. These links emulate a form submission, as shown in Listing 6.

Listing 6—Linking to the Day's Events (calendar.php)

<html>
<head><title>Event Calendar</title></head>
<body>
<form action="calendar.php" method="get">
  Choose a month:
  <select name="month">
    <option value="1">January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
  </select>
  /
  <select name="year">
    <option value="2002">2002</option>
    <option value="2003">2003</option>
    <option value="2004">2004</option>
    <option value="2005">2005</option>
  </select>
  <input type="submit" value="Display Calendar" />
</form>
<?php

  //Initialize variables
  import_request_variables('pgc', '');
  if (!isset($month)) {
    $today = getdate();
    $month = $today['mon'];
    $year = $today['year'];
  }

  $numberofdays = cal_days_in_month(CALENDAR_GREGORIAN, $month, $year);

  $daysoftheweek = array("SUNDAY", "MONDAY", "TUESDAY",
              "WEDNESDAY", "THURSDAY", "FRIDAY",
              "SATURDAY");
  echo ("<h1>This Month's Events ($month/$year)</h1>");
?>
  <table border="1">
    <tr>
<?php
  //Set headings
  foreach ($daysoftheweek as $thisday) {
    echo("<th>$thisday</th>");
  }
?>
    </tr>
    <tr>
<?php

  //Align first day
  $daynum = jddayofweek(gregoriantojd($month, 1, $year), 0);
  for ($blankdays = 0; $blankdays < $daynum; $blankdays++){
    echo("<td>&nbsp;</td>");
  }

  //Display the rest of the month
  for ($i = 1; $i <= $numberofdays; $i++) {

    //If it's a Sunday, start a new row; otherwise, just display
    $thisdayofweek = jddayofweek(gregoriantojd($month, $i, $year), 0);
    if ( $thisdayofweek == 0) {
     echo("</tr><tr><td>");
     echo("<a href=\"showday.php?month=$month&year=$year&day=$i\">");
     echo($i);
     echo("</a>");
     echo("</td>");
    } else {
     echo("<td>");
     echo("<a href=\"showday.php?month=$month&year=$year&day=$i\">");
     echo($i);
     echo("</a>");
     echo("</td>");  
    }
  }

  //Complete the month
  $julianday = gregoriantojd($month, $numberofdays, $year);
  $daynum = jddayofweek($julianday, 0);
  for ($blankdays = $daynum+1; $blankdays < 7; $blankdays++){
    echo("<td>&nbsp;</td>");
  }

?>
    </tr>
  </table>
</body>
</html>

I've reproduced the entire file for the benefit of those who skipped Part 1 of this series, but the important part is simply that we've added links to the numbers being displayed. The links have the month, day, and year embedded, so if we click December 14, 2002, the browser goes to

http://localhost/showday.php?month=12&year=2002&day=14

Look familiar? It's the same URL we would have gotten if we'd created a form that used the GET method for submission. The benefit here is that we can then access that data just as though it had come from a form, as shown in Listing 7.

Listing 7—Completing the Event List (showday.php)

<html>
<head><title>Events for the day</title></head>
<body>
<?php

// Connect to database.
$connection = mysql_connect ( "localhost", "myusername", "mypassword" ) ||
 die ( "Error connecting to database!" );

import_request_variables('g', '');

echo("<h2>Events for $month/$day/$year:</h2>");

$select_statement = "SELECT eventid, eventTitle from events ".
   "where eventMonth=$month and eventDay=$day and eventYear=$year";

$results = mysql_db_query ( "mysql", $select_statement );

while ($event = mysql_fetch_array ( $results )) {

   printf ( "<a href=\"showevent.php?eventid=%s\">%s</a><br />",
        $event["eventid"], $event["eventTitle"] );

}

// Free resources.
mysql_free_result ( $results );

?>
</body>
</html>

We've made only two changes to the page here:

  • Rather than explicitly setting $month, $day, and $year, we're importing the values just as though they had come from a form.

  • We're using printf() instead of echo() to output the information. The printf() function takes as its first argument a template—in this case, the link—with %s representing string "placeholders." These placeholders are filled by each of the subsequent arguments, in order. So the first %s is replaced by the eventid, and the second by the eventTitle. Figure 6 shows the results.

Figure 6Figure 6 Using printf() formats the output.

We can use the same principle to display the detailed information about the event. Listing 8 shows the contents of the showevent.php page, with the results shown in Figure 7.

Listing 8—Displaying Information about the Event (showevent.php)

<html>
<head><title>Event Information</title></head>
<body>
<?php

// Connect to database.
$connection = mysql_connect ( "localhost", "myusername", "mypassword" ) ||
 die ( "Error connecting to database!" );

import_request_variables('g', '');

$select_statement = "SELECT eventTitle, eventDesc, eventMonth, eventDay, eventYear ".
          "from events where eventid=$eventid";

$results = mysql_db_query ( "mysql", $select_statement );

while ($event = mysql_fetch_array ( $results )) {

   printf ( "<h2>%s</h2>", $event["eventTitle"] );
   printf ( "<h3>Date: %s/%s/%s</h3>",
          $event["eventMonth"], $event["eventDay"], $event["eventYear"] );
   printf ( "<p>%s</p>", $event["eventDesc"] );

}

// Free resources.
mysql_free_result ( $results );

?>
</body>
</html>

Figure 7Figure 7 Event details can be retrieved just as the titles were.

  • + Share This
  • 🔖 Save To Your Account