Viewing and Editing Tasks for a Project Management Application

Welcome to the fifth part of a seven part series on building a project management application. In the last article we looked at the view_tasks.php script. Specifically, we looked at the PHP portion of the script in detail. In this article we will finish discussing that script by examining the HTML portion. We will then move on to discuss the edit_task.php script, which is responsible for handling any changes that the user makes to a task.

To create the dynamic HTML table for the view task script, PHP is going to use the $num variable that it stored in the PHP portion of the page. The table headers are:

  • Description
  • To be completed by

These headers are static and will not be affected by the dynamic aspect of the code. Let’s look at how it is done:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><!– InstanceBegin template="/Templates/PM_Main.dwt.php" codeOutsideHTMLIsLocked="false" –>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<!– InstanceBeginEditable name="doctitle" –>

<title>Untitled Document</title>

<!– InstanceEndEditable –>

<!– InstanceBeginEditable name="head" –>

<!– InstanceEndEditable –>

<link href="Templates/main.css" rel="stylesheet" type="text/css" />

</head>


<body>

<table width="100%" border="0">

<tr>

<td width="33%">&nbsp;</td>

<td width="28%">&nbsp;</td>

<td width="39%">Logged in: <!– InstanceBeginEditable name="login" –><? echo $_SESSION['name'];?> | <a href="logout.php">Logout</a><!– InstanceEndEditable –></td>

</tr>

<tr>

<td colspan="3" bgcolor="#6699CC" class="headertxt">Project Management Software </td>

</tr>

<tr>

<td colspan="3"><!– InstanceBeginEditable name="main" –>

<table width="100%" border="0">

<tr>

<td colspan="2" class="loginheader"><?php echo $title;?> </td>

</tr>

<tr>

<td>&nbsp;</td>

<td>&nbsp;</td>

</tr>

<tr>

<td>Task Description: </td>

<td>To be completed by: </td>

</tr>


The dynamic rows start here. First, PHP checks to see how many table rows it will need to create:


<?php

if($num > 0){

Then it creates a while() loop that will actually create rows based on the number of results. The $num variable stores a number, so if the number is, for example, five, then the number of rows that are created will also be five. Now, as these rows are created, PHP adds the description and "complete by" dates to the table:


while($row = mysql_fetch_assoc($result)){?>

<tr>

<td><?php echo $row['task_description']?></td>

<td><?php echo $row['complete_by']?></td>

</tr>

<?php

}


If the $num variable does not contain any numbers, it means that no tasks were available for the given project. An appropriate message will be given:


}else{

?>

 

<tr>

<td colspan="2"><p>There are no tasks registered for this project.</p></td>

</tr>

<?php }

 

?>

</table>

<!– InstanceEndEditable –></td>

</tr>

<tr>

<td colspan="3"><!– InstanceBeginEditable name="nav" –><table width="100%" border="0">


The view tasks page has two navigation links at the bottom that take you either to the Administrator’s corner or the edit tasks page. We are going to be looking at the edit tasks page in the next section:


<tr>

<td><a href="edit_task.php?pid=<?php echo $_GET['pid'];?>">Edit Task</a> | <a href="admin/login.php">Administrators Corner </a></td>

</tr>

</table><!– InstanceEndEditable –></td>

</tr>

<tr>

<td align="right" class="cright" colspan="3">copyright &copy; 2007 PM </td>

</tr>

</table>

</body>

<!– InstanceEnd –></html>

{mospagebreak title=The edit_task script}

The edit_task.php script is responsible for updating the tasks table whenever you make any changes to a task. Its main role is to display an HTML form that contains the information about the task you want to change and then sends that information to the database. Below is a screen shot of the edit_task.php page:

And here’s the code for the entire script:


<?php

include "dbcon.php";

include "functions.php";

$cpid=mysql_escape_String($_GET['pid']);

$tpid=mysql_escape_String($_GET['tid']);



$getname = "SELECT title FROM projects WHERE pid = ‘".$cpid."’";

$g_result = mysql_query($getname);

if(!$g_result){

echo mysql_error();

}else{

$rowname = mysql_fetch_assoc($g_result);

$title = $rowname['title'];

}


$gettask= "SELECT * FROM tasks WHERE tid = ‘".$ctid."’";

$t_result = mysql_query($gettask);

if(!$t_result){

echo mysql_error();

}else{

$rowtask = mysql_fetch_assoc($t_result);

}



if(isset($_POST['submit'])){

//check vars

$descr=mysql_escape_string($_POST['descr']);

$p_pid=mysql_escape_string($_POST['p_pid']);


//build date

$duedt = $_POST['yy'] . "-";

if($_POST['mm'] < 10) {

$duedt .= "0";

}

$duedt .= $_POST['mm'] . "-";

if($_POST['mm'] == 4 || $_POST['mm'] == 6 || $_POST['mm'] == 9 || $_POST['mm'] == 11) {

if($_POST['dd'] > 30) {

$duedt .= "30";

} else {

$duedt .= $_POST['dd'];

}

} elseif($_POST['mm'] == 2) {

if($_POST['yy'] == 2008 || $_POST['yy'] == 2012) {

if($_POST['dd'] > 29) {

$duedt .= "29";

} else {

$duedt .= $_POST['dd'];

}

} else {

if($_POST['dd'] > 28) {

$duedt .= "28";

} else {

$duedt .= $_POST['dd'];

}

}

} else {

$duedt .= $_POST['dd'];

}

 

//insert

$insert = "INSERT INTO tasks SET task_description = ‘".$descr."’,";

$insert .= "complete_by = ‘".$duedt."’,p_id= ‘".$p_pid."’";

if(!mysql_query($insert)){

echo mysql_error();

}

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><!– InstanceBegin template="/Templates/PM_Main.dwt.php" codeOutsideHTMLIsLocked="false" –>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<!– InstanceBeginEditable name="doctitle" –>

<title>Untitled Document</title>

<!– InstanceEndEditable –>

<!– InstanceBeginEditable name="head" –>

<!– InstanceEndEditable –>

<link href="Templates/main.css" rel="stylesheet" type="text/css" />

</head>


<body>

<table width="100%" border="0">

<tr>

<td width="33%">&nbsp;</td>

<td width="28%">&nbsp;</td>

<td width="39%">Logged in: <!– InstanceBeginEditable name="login" –><? echo $_SESSION['name'];?> | <a href="logout.php">Logout</a><!– InstanceEndEditable –></td>

</tr>

<tr>

<td colspan="3" bgcolor="#6699CC" class="headertxt">Project Management Software </td>

</tr>

<tr>

<td colspan="3"><!– InstanceBeginEditable name="main" –>

<form id="form1" name="form1" method="post" action="edit_task.php">

<table width="100%" border="0">

<tr>

<td colspan="2" class="loginheader"><?php echo $title;?></td>

</tr>

<tr>

<td width="14%">&nbsp;</td>

<td width="86%">&nbsp;</td>

</tr>

<tr>

<td valign="top">Description</td>

<td><label>

<textarea name="descr"><?php echo $rowtask['task_description'];?></textarea>

</label></td>

</tr>

<tr>

<td valign="top">Complete by: </td>

<td><label>

<?

$dd = date("d");

$mm = date("m");

$yy = date("Y");

echo "<select name="dd">n";

for($i = 1; $i <= 31; $i++) {

echo "<option value="" . $i . """;

if($i == $dd) {

echo " selected";

}

echo ">" . $i . "</option>n";

}

echo "</select>&nbsp;<select name="mm">n";

for($i = 1; $i <= 12; $i++) {

echo "<option value="" . $i . """;

if($i == $mm) {

echo " selected";

}

echo ">" . $month_names[$i] . "</option>n";

}

echo "</select>&nbsp;<select name="yy">n";

for($i = $yy; $i <= ($yy + 1); $i++) {

echo "<option value="" . $i . """;

if($i == $yy) {

echo " selected";

}

echo ">" . $i . "</option>n";

}

echo "</select>";

?>

</label>

<input name="p_pid" type="hidden" value="<?php echo $_GET['pid']?>" /></td>

</tr>

<tr>

<td>&nbsp;</td>

<td><label>

<input name="submit" type="submit" id="submit" value="Update Task" />

</label></td>

</tr>

</table>

</form>

 

<!– InstanceEndEditable –></td>

</tr>

<tr>

<td colspan="3"><!– InstanceBeginEditable name="nav" –><table width="100%" border="0">

<tr>

<td><a href="main.php">View Project List</a> | <a href="admin/login.php">Administrators Corner </a></td>

</tr>

</table><!– InstanceEndEditable –></td>

</tr>

<tr>

<td align="right" class="cright" colspan="3">copyright &copy; 2007 PM </td>

</tr>

</table>

</body>

<!– InstanceEnd –></html>

{mospagebreak title=Script Explained}

So let’s look at the code:


<?php

include "dbcon.php";

include "functions.php";


The code cleans the two IDs that it receives, before using them in queries. The "tid" is a task ID and the "pid" is a project ID. Ideally, we would not need the project ID, but we need it to retrieve the project name in order to display it on the form. Also, without the name of the project prominently displayed across the form, some users might forget which project they are editing the tasks for:


$cpid=mysql_escape_String($_GET['pid']);

$tpid=mysql_escape_String($_GET['tid']);


First we retrieve the title of the project from the projects table, using the project ID that we received, and store the name in the $title variable:


$getname = "SELECT title FROM projects WHERE pid = ‘".$cpid."’";

$g_result = mysql_query($getname);

if(!$g_result){

echo mysql_error();

}else{

$rowname = mysql_fetch_assoc($g_result);

$title = $rowname['title'];

}


Next, the code retrieves the specific task based on the task ID rather than the project ID. There might be more than one task with the same project ID, so it is safer to do it this way:


$gettask= "SELECT * FROM tasks WHERE tid = ‘".$ctid."’";

$t_result = mysql_query($gettask);

if(!$t_result){

echo mysql_error();

}else{


The result of the query is stored in the "$rowtask" variable:


$rowtask = mysql_fetch_assoc($t_result);

}


We run this query to populate the HTML form with the task data. The next part of the code deals with updating the task. It receives the form data, does the security checks, and then runs the query to update the table:


if(isset($_POST['submit'])){

//check vars

$descr=mysql_escape_string($_POST['descr']);

$p_pid=mysql_escape_string($_POST['p_pid']);


//build date

$duedt = $_POST['yy'] . "-";

if($_POST['mm'] < 10) {

$duedt .= "0";

}

$duedt .= $_POST['mm'] . "-";

if($_POST['mm'] == 4 || $_POST['mm'] == 6 || $_POST['mm'] == 9 || $_POST['mm'] == 11) {

if($_POST['dd'] > 30) {

$duedt .= "30";

} else {

$duedt .= $_POST['dd'];

}

} elseif($_POST['mm'] == 2) {

if($_POST['yy'] == 2008 || $_POST['yy'] == 2012) {

if($_POST['dd'] > 29) {

$duedt .= "29";

} else {

$duedt .= $_POST['dd'];

}

} else {

if($_POST['dd'] > 28) {

$duedt .= "28";

} else {

$duedt .= $_POST['dd'];

}

}

} else {

$duedt .= $_POST['dd'];

}

 

//insert

$update = "UPDATE tasks SET task_description = ‘".$descr."’,";

$update .= "complete_by = ‘".$duedt."’,p_id= ‘".$p_pid."’";

if(!mysql_query($update)){

echo mysql_error();

}

}

?>

{mospagebreak title=HTML Form}

The HTML form is exactly the same as the form used in the add_task.php script, except the form fields now have the values from the query. So when you see the form, all the information about the task that you want to change will already be on it. I’ve highlighted those values:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><!– InstanceBegin template="/Templates/PM_Main.dwt.php" codeOutsideHTMLIsLocked="false" –>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<!– InstanceBeginEditable name="doctitle" –>

<title>Untitled Document</title>

<!– InstanceEndEditable –>

<!– InstanceBeginEditable name="head" –>

<!– InstanceEndEditable –>

<link href="Templates/main.css" rel="stylesheet" type="text/css" />

</head>


<body>

<table width="100%" border="0">

<tr>

<td width="33%">&nbsp;</td>

<td width="28%">&nbsp;</td>

<td width="39%">Logged in: <!– InstanceBeginEditable name="login" –><? echo $_SESSION['name'];?> | <a href="logout.php">Logout</a><!– InstanceEndEditable –></td>

</tr>

<tr>

<td colspan="3" bgcolor="#6699CC" class="headertxt">Project Management Software </td>

</tr>

<tr>

<td colspan="3"><!– InstanceBeginEditable name="main" –>

<form id="form1" name="form1" method="post" action="edit_task.php">

<table width="100%" border="0">

<tr>

<td colspan="2" class="loginheader"><?php echo $title;?></td>

</tr>

<tr>

<td width="14%">&nbsp;</td>

<td width="86%">&nbsp;</td>

</tr>

<tr>

<td valign="top">Description</td>

<td><label>

<textarea name="descr"><?php echo $rowtask['task_description'];?></textarea>

</label></td>

</tr>

<tr>

<td valign="top">Complete by: </td>

<td><label>

<?

$dd = date("d");

$mm = date("m");

$yy = date("Y");

echo "<select name="dd">n";

for($i = 1; $i <= 31; $i++) {

echo "<option value="" . $i . """;

if($i == $dd) {

echo " selected";

}

echo ">" . $i . "</option>n";

}

echo "</select>&nbsp;<select name="mm">n";

for($i = 1; $i <= 12; $i++) {

echo "<option value="" . $i . """;

if($i == $mm) {

echo " selected";

}

echo ">" . $month_names[$i] . "</option>n";

}

echo "</select>&nbsp;<select name="yy">n";

for($i = $yy; $i <= ($yy + 1); $i++) {

echo "<option value="" . $i . """;

if($i == $yy) {

echo " selected";

}

echo ">" . $i . "</option>n";

}

echo "</select>";

?>

</label>

<input name="p_pid" type="hidden" value="<?php echo $_GET['pid']?>" /></td>

</tr>

<tr>

<td>&nbsp;</td>

<td><label>

<input name="submit" type="submit" id="submit" value="Update Task" />

</label></td>

</tr>

</table>

</form>

 

<!– InstanceEndEditable –></td>

</tr>

<tr>

<td colspan="3"><!– InstanceBeginEditable name="nav" –><table width="100%" border="0">

<tr>

<td><a href="main.php">View Project List</a> | <a href="admin/login.php">Administrators Corner </a></td>

</tr>

</table><!– InstanceEndEditable –></td>

</tr>

<tr>

<td align="right" class="cright" colspan="3">copyright &copy; 2007 PM </td>

</tr>

</table>

</body>

<!– InstanceEnd –></html>

That’s it for now.  Be sure to check in for the next article of the series.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye