Home arrow PHP arrow Page 7 - Time is Money (part 1)

Today's Menu - PHP

For consultancies that bill on an hourly basis - lawyers,accountants et al - time tracking is a critical part of the billingprocess. For small- and medium-size organizations, resource tracking,allocation and analysis is essential for business efficiency and planning.This article addresses both requirements by teaching you how to build atimesheet system to track and analyze work hours with PHP and MySQL.

  1. Time is Money (part 1)
  2. Up A Creek
  3. Bills, Bills, Bills
  4. So Many Tables, So Little Time
  5. Open Sesame
  6. The Lazy Programmer Strikes Again
  7. Today's Menu
  8. Too Much Information
  9. Time For Bed
By: The Disenchanted Developer, (c) Melonfire
Rating: starstarstarstarstar / 5
October 22, 2001

print this article


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: