PHP
  Home arrow PHP arrow Page 3 - Miles To Go Before I Sleep...
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Miles To Go Before I Sleep...
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2001-02-05


    Table of Contents:
  • Miles To Go Before I Sleep...
  • Building The Foundation
  • Seven Days, Seven Nights
  • Bringing In The Database
  • Adding, Editing, Deleting...
  • The Final Touch

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Miles To Go Before I Sleep... - Seven Days, Seven Nights
    ( Page 3 of 6 )

    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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT