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"><all projects></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:
Next: Too Much Information >>
More PHP Articles
More By The Disenchanted Developer, (c) Melonfire