PHP
  Home arrow PHP arrow Page 7 - Time is Money (part 1)
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

Time is Money (part 1)
By: The Disenchanted Developer, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2001-10-22


    Table of Contents:
  • Time is Money (part 1)
  • Up A Creek
  • Bills, Bills, Bills
  • So Many Tables, So Little Time
  • Open Sesame
  • The Lazy Programmer Strikes Again
  • Today's Menu
  • Too Much Information
  • Time For Bed

  • 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


    Time is Money (part 1) - Today's Menu
    ( Page 7 of 9 )

    The page header and footer enclose the code necessary to perform the particular script's function. In the specific case of "menu.php", this code involves setting up a main menu for the user to:

    view a list of current projects, with descriptions;

    view a list of standard tasks, with definitions;

    view or add timesheet data for any particular day;

    generate reports (if administrator);

    log out of the system;

    Here's the code to display these options:

    <? // display page header $title = "Main Menu"; include("header.inc.php"); ?> Please select from the following options: <ul> <li> <!-- view projects option --> <a href="projects.php">View project descriptions</a> <p> <li> <!-- view tasks option --> <a href="tasks.php">View task descriptions</a> <p> <li> <!-- view timesheet option --> <form name="view" action="view.php" method="post"> <a href="javascript:submitForm(0)">View timesheet</a> for <? generateDateSelector(); ?> </form> <p> <li> <!-- log out option --> <a href="logout.php">Log out of the system </a> </ul> <? include("footer.inc.php"); ?>
    Here's what it looks like:



    This is nothing but an unordered list of links, with each link pointing to a different script. Notice that the middle link requires the user to select a date, and so I've constructed it as a separate form containing a series of date selection boxes. I've also done away with the standard submit buttons in this form, preferring instead to use a simple JavaScript to submit it when its corresponding link is clicked.

    In case you're wondering about the call to the generateDateSelector() function, let me explain what it does. Since I will be using the same series of drop-down boxes for date selection in numerous places, I decided to save myself some time by writing a simple PHP function to generate these boxes for me on demand. This function is stored in the include()d file "functions.php", and looks like this:

    <? // generate three list boxes for d-m-y selection function generateDateSelector($prefix="") { // month array $monthArray = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); // get current year, month and date $arr = getdate(mktime()); $currYear = $arr["year"]; $currMonth = $arr["mon"]; $currDay = $arr["mday"]; // generate date drop-down echo "<select name=" . $prefix . "d>"; for ($x=1; $x<=31; $x++) { $str = "<option value=" . sprintf("%02d", $x) . ""; if ($x == $currDay) { $str .= " selected"; } $str .= ">" . sprintf("%02d", $x) . "</option>"; echo $str; } echo "</select>"; // generate month drop-down echo "<select name=" . $prefix . "m>"; for ($x=1; $x<=12; $x++) { $str = "<option value=" . sprintf("%02d", $x) . ""; if ($x == $currMonth) { $str .= " selected"; } $str .= ">" . $monthArray[$x] . "</option>"; echo $str; } echo "</select>"; // generate year drop-down echo "<select name=" . $prefix . "y>"; for ($x=$currYear; $x<($currYear+5); $x++) { $str = "<option value=$x"; if ($x == $currYear) { $str .= " selected"; } $str .= ">" . sprintf("%04d", $x) . "</option>"; echo $str; } echo "</select>"; } ?>
    As you can see, the function includes code to automatically pre-select the current date, month and year, together with an optional $prefix argument to customize the variable names for the three drop-down boxes.

    The output of this function would look something like this

    <select name=d> <option value=01>01</option><option value=02>02</option><option value=03>03</option><option value=04>04</option><option value=05>05</option><option value=06>06</option><option value=07>07</option><option value=08>08</option><option value=09>09</option><option value=10>10</option><option value=11>11</option><option value=12>12</option><option value=13>13</option><option value=14>14</option><option value=15>15</option><option value=16>16</option><option value=17>17</option><option value=18>18</option><option value=19>19</option><option value=20>20</option><option value=21>21</option><option value=22>22</option><option value=23 selected>23</option><option value=24>24</option><option value=25>25</option><option value=26>26</option><option value=27>27</option><option value=28>28</option><option value=29>29</option><option value=30>30</option><option value=31>31</option> </select> <select name=m> <option value=01>January</option><option value=02>February</option><option value=03>March</option><option value=04>April</option><option value=05>May</option><option value=06>June</option><option value=07>July</option><option value=08 selected>August</option><option value=09>September</option><option value=10>October</option><option value=11>November</option><option value=12>December</option> </select> <select name=y> <option value=2001 selected>2001</option><option value=2002>2002</option><option value=2003>2003</option><option value=2004>2004</option><option value=2005>2005</option> </select>
    or this:



    There's only one thing missing from this menu - the link for administrators to use when generating reports. However, I need to check that the user currently logged-in is, in fact, an administrator before displaying this menu option. Let's take care of that next:

    <!-- view timesheet option - snip --> <? // if administrator logged-in // display report option if ($SESSION_UPERMS == 1) { ?> <li> <!-- generate report option --> <form name="report" action="report.php" method="post"> <a href="javascript:submitForm(1)">Generate activity reports</a> between <? generateDateSelector("s"); ?> and <? generateDateSelector("e"); ?> for <select name="pid"> <option value="0">&lt;all projects&gt;</option> <? // open connection to database $connection = mysql_connect($hostname, $user, $pass) or die ("Unable to connect!"); // get project list $query = "SELECT pid, pname from projects"; $result = mysql_db_query($database, $query, $connection) or die ("Error in query: $query. " . mysql_error()); while (list($pid, $pname) = mysql_fetch_row($result)) { echo "<option value=$pid>$pname</option>"; } mysql_free_result($result); ?> </select> </form> <p> <? } ?> <!-- log out option - snip -->
    Since I would like administrators to have the ability to view reports for a specific project, I've included a drop-down box containing a list of all current projects in the menu item above. This list is generated via a query to the "projects" table.

    And here's what the finished product looks like:



     
     
    >>> More PHP Articles          >>> More By The Disenchanted Developer, (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