Home arrow PHP arrow Page 3 - Miles To Go Before I Sleep...

Seven Days, Seven Nights - PHP

Ever wished you had an appointment calendar you could accessthrough your Web browser? Well, it's not too late. This week, learn how tobuild a simple PHP-based appointment calendar which allows you to view,add, edit and delete appointments and meetings. And if you're just gettinginto PHP, this article will show you how to apply the theory you've spentso much time learning to a real-world application.

TABLE OF CONTENTS:
  1. Miles To Go Before I Sleep...
  2. Building The Foundation
  3. Seven Days, Seven Nights
  4. Bringing In The Database
  5. Adding, Editing, Deleting...
  6. The Final Touch
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 6
February 05, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Let's move to the calendar display proper:

<? <table border="0" cellpadding="2" cellspacing="5"> <!-- month display --> <tr> <td><font face="Arial" size="-2"><<</font></td> <td colspan="5" align="CENTER"><font face="Arial" size="-1"><b><? echo $months[$currMonth] . " " . $currYear; ?></b></font></td> <td><font face="Arial" size="-2">>></font></td> </tr> <!-- day names --> <tr> <? for ($x=0; $x<7; $x++) { echo "<td><font face=Arial size=-2>" . substr($days[$x],0,3) . "</font></td>"; } ?> </tr> <!-- start displaying dates --> <tr> <? // display blank spaces until the first day of the month for ($x=1; $x<=$firstDayOfMonth; $x++) { // this comes in handy to find the end of each 7-day block $rowCount++; echo "<td><font face=Arial size=-2> </font></td>\n"; } // counter to track the current date $dayCount=1; while ($dayCount <= $totalDays[$currMonth]) { // use this to find out when the 7-day block is complete and display a new row if ($rowCount % 7 == 0) { echo "</tr>\n<tr>\n"; } // if today, display in different colour // print date if ($dayCount == date("j") && $currYear == date("Y") && $currMonth == date("n")) { echo "<td align=center bgcolor=Silver><font face=Arial size=-1>" . $dayCount. "</font>"; } else { echo "<td align=center><font face=Arial size=-1>" . $dayCount . "</font>"; } echo "</td>\n"; // increment counters $dayCount++; $rowCount++; } ?> </tr> <tr> <td align=right colspan=7> <font face=Arial size=-2> <a href="<? echo $PHP_SELF; ?>">this month</a> </font> </td> </tr> </table> ?>
I'll explain this table row by row. The first row contains "next" and "previous" links (inactive at this point), to allow the user to navigate to the next or previous month of the year, with the name of the current month sandwiched in between.

The next row contains seven cells, one for each day of the week -I've used the substr() function to display the first three letters of each day name from the $days array.

The next few rows are all generated automatically. The first order of business is to place the first day of the month on the corresponding day. Since I already have $firstDayOfMonth variable, I've used a simple loop to fill all the cells prior to that day with non-breaking spaces.

<? // display blank spaces until the first day of the month for ($x=1; $x<=$firstDayOfMonth; $x++) { // this comes in handy to find the end of each 7-day block $rowCount++; echo "<td><font face=Arial size=-2> </font></td>\n"; } ?>
The $rowCount variable is simultaneously keeping track of the number of slots (cells) being filled up - I'll use this a little further down to determine when the end of the week has been reached.

Once the first day of the month is determined, another "for" loop (iterating from 1 to $totalDays[$currMonth]) is used to generate the remaining rows and cells of the table. The $rowCount and $dayCount variables are incremented at each stage, and the $rowCount variable is divided by 7 to find out when the seven slots available in each row are filled up.

<? // counter to track the current date $dayCount=1; while ($dayCount <= $totalDays[$currMonth]) { // use this to find out when the 7-day block is //complete and display a new row if ($rowCount % 7 == 0) { echo "</tr>\n<tr>\n"; } // if today, display in different colour // print date if ($dayCount == date("j") && $currYear == date("Y") && $currMonth == date("n")) { echo "<td align=center bgcolor=Silver><font face=Arial size=-1>" . $dayCount. "</font>"; } else { echo "<td align=center><font face=Arial size=-1>" . $dayCount . "</font>"; } echo "</td>\n"; // increment counters $dayCount++; $rowCount++; } ?>

I've inserted an "if" statement into the loop to display the current date in a different colour, if a match is found. And the last row of the table simply contains a link to "this month" - in case you're checking out April 2030 and need a quick way to get back to the present day.

Here's what the result looks like.


This article copyright Melonfire 2001. All rights reserved.{mospagebreak title=January To December, And Everything In Between} Now, by itself, this isn't very useful, since it only displays information for the current month (as determined by the server's clock). My next task, therefore, is to make it possible to move forwards and backwards through the year, by activating the "next" and "previous" links on the first row.

In order to do this, I need to define a few new variables, which will be used to identify the previous and next month and year.

<? // set up variables to display previous and next months correctly // defaults for previous month $prevMonth = $currMonth-1; $prevYear = $currYear; // if January, decrement year and set month to December if ($prevMonth < 1) { $prevMonth=12; $prevYear--; } // defaults for next month $nextMonth = $currMonth+1; $nextYear = $currYear; // if December, increment year and set month to January if ($nextMonth > 12) { $nextMonth=1; $nextYear++; } ?>
Note the correction that has to take place if the month in question is either January or December.

Once those variables are defined, it's a simple matter to activate the links on the top row of the calendar. The variables are passed back to the script using the GET method.

<? <table border="0" cellpadding="2" cellspacing="5"> <!-- month display --> <!-- this is the first row of the calendar, with links active --> <tr> <td><a href="<? echo $PHP_SELF; ?>?currMonth=<? echo $prevMonth; ?>&currYear=<? echo $prevYear; ?>"><font face="Arial" size="-2"><<</font></a></td> <td colspan="5" align="CENTER"><font face="Arial" size="-1"><b><? echo $months[$currMonth] . " " . $currYear; ?></b></font></td> <td><a href="<? echo $PHP_SELF; ?>?currMonth=<? echo $nextMonth; ?>&currYear=<? echo $nextYear; ?>"><font face="Arial" size="-2">>></font></font></a></td> </tr> ?>
You should now have a calendar capable of displaying information for any month of any year.

The next task is to convert each date on the calendar into an active link, which, when clicked, will display the user's current appointments for that day, together with the option to add new appointments. This intelligence will be built into a file called "day.view.php", which requires three parameters - the year, month and date under consideration. So let's make that modification to the code above:

<? // if today, display in different colour // print date, each date is now an active hyperlink if ($dayCount == date("j") && $currYear == date("Y") && $currMonth == date("n")) { echo "<td align=center bgcolor=Silver><font face=Arial size=-1><a href=day.view.php?currYear=" . $currYear . "&currMonth=" . $currMonth . "&currDay=" . $dayCount . ">" . $dayCount. "</a></font>"; } else { echo "<td align=center><font face=Arial size=-1><a href=day.view.php?currYear=" . $currYear . "&currMonth=" . $currMonth . "&currDay=" . $dayCount . ">" . $dayCount . "</a></font>"; } ?>

All done? Here's what it looks like.



Let's move on to the "day view".

This article copyright Melonfire 2001. All rights reserved.

 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: