Making Changes in a Project Management Application

This is the third part of a seven part article series detailing the creation of a project management application. It will discuss how to make changes to the project, such as the project’s status, via the edit_project.php script.

The next script that we will be looking at is the edit_project.php script. It is responsible for handling any changes that you want to make to a project. It has exactly the same form layout as the add_project.php script, except it needs to do a retrieve and update query. The reason for retrieving the project information in the first place is to display that information in a form. This way you know and see exactly what you want to change.

For example, if the project name is ‘ProjectX’ and you want to change it to ‘My Project,’ then all you do is delete the ‘Project X’ name and add the ‘My Project’ name to that field. It just makes the chances of someone making the wrong entry very slim. Once you have changed the things you want, you simply click on the update button and the update query will spring into action. Below is a screen shot of what the edit_project page looks like:


Below is the entire code for this page:

<?php

include "dbcon.php";

include "functions.php";

//initialise variables

$alert =false;

// retrieve information based on the user id, that we set in the login page:


if(isset($_GET['pid'])){

//clean pid

if(!is_numeric($_GET['pid'])){

//the value received is not numeric. redirect the user to login

header("location:login.php");

}


//otherwise clean the received value for query use

//get projects

$projectID = mysql_escape_string($_GET['pid']);

$getproject= "SELECT * FROM projects WHERE pid = ‘".$projectID."’";

$results = mysql_query($getproject);

$projectdetails = mysql_fetch_assoc($results);

}



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


//clean vars

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

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

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

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

$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'];

}


//update

$query = "UPDATE projects SET title=’" .$title. "’, ";

$query .= "project_description=’" . $descr. "’, status=’" .$status . "’,due_dt=’" .$duedt."’,";

$query .= "create_dt=’" . $createdt. "’, u_id=’" .$_SESSION['uid'] . "’";

$query .= " WHERE pid=’" .$p_pid. "’";

$result=mysql_query($query);

if(!$result){

echo mysql_error();

}else{

header("location:main.php");

}


}//submit

?>


<!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_project.php" enctype="multipart/form-data">

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

<tr>

<td width="12%">proj name </td>

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

</tr>

<tr>

<td>Title</td>

<td><label>

<input name="title" type="text" id="title" value="<?php echo $projectdetails['title']?>"/>

</label></td>

 

</tr>

<tr>

<td>Description</td>

<td><label>

<textarea name="descr" id="descr"><?php echo $projectdetails['project_description']?></textarea>

</label></td>

</tr>

<tr>

<td>Status</td>

<td><label>

<select name="status" id="status">

<?php

$list=array(‘overdue’,’completed’,’pending’);

switch($projectdetails['status']){

 

case "overdue":

echo "<option value=’overdue’

selected";

 

break;

case "completed":

echo "<option value=’completed’

selected";

 

break;

case "pending":

echo "<option value=’pending’

selected";

 

break;

}

for($x=0; $x < 4; $x++){

echo ">" .$list[$x]. "</option>";

}

?>

</select>

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

</label></td>

</tr>

<tr>

<td>Date Due </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>";

?>

<input type="hidden" name="createdt" value="<?php echo $projectdetails['create_dt']?>" />

</label></td>

</tr>

<tr>

<td>&nbsp;</td>

<td><label>

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

</label></td>

</tr>

</table>


</form>

<!– InstanceEndEditable –></td>

</tr>

<tr>

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

<tr>

<td><a href="edit_task.php?pid=<?php echo $projectdetails['pid'];?>">Change a Task in this Project</a> | <a href="edit_staff.php?pid=<?php echo $projectdetails['pid'];?>">Change Staff members of this Project </a> | <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=PHP Explained}

Let’s take a closer look at the PHP section of the code. The HTML is not so difficult to understand, so I will just review it briefly later on.

The first part, as always, includes the functions and dbcon.php files. It then has a place to initialize variables:


<?php

include "dbcon.php";

include "functions.php";

//initialise variables

$alert =false;

// retrieve information based on the user id, that we set in the login page:


The project id variable that is received is checked to see if it is a number. If it is not a number, then we know someone has tampered with it and we need to redirect the user to the login page:


if(isset($_GET['pid'])){

//clean pid

if(!is_numeric($_GET['pid'])){

//the value recieved is not numeric. redirect the user to login

header("location:login.php");

}


If all is well, we continue to retrieve the project details and store them in the $projectdetails variable:


//otherwise clean the recieved value for query use

//get projects

$projectID = mysql_escape_string($_GET['pid']);

$getproject= "SELECT * FROM projects WHERE pid = ‘".$projectID."’";

$results = mysql_query($getproject);

$projectdetails = mysql_fetch_assoc($results);

}

The code checks to see if the form has been submitted. This would indicate that the user has made his or her changes and is now ready to update the project details:


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


//clean vars

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

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

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

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

$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'];

}

We run an update query and update the project’s table:


//update

$query = "UPDATE projects SET title=’" .$title. "’, ";

$query .= "project_description=’" . $descr. "’, status=’" .$status . "’,due_dt=’" .$duedt."’,";

$query .= "create_dt=’" . $createdt. "’, u_id=’" .$_SESSION['uid'] . "’";

$query .= " WHERE pid=’" .$p_pid. "’";

$result=mysql_query($query);

if(!$result){

echo mysql_error();

}else{

header("location:main.php");

}


}//submit

?>

{mospagebreak title=HTML Form}

In the HTML form below, we are going to briefly look at how the status of the project is determined:


<!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_project.php" enctype="multipart/form-data">

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

<tr>

<td width="12%">proj name </td>

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

</tr>

<tr>

<td>Title</td>

<td><label>

<input name="title" type="text" id="title" value="<?php echo $projectdetails['title']?>"/>

</label></td>

 

</tr>

<tr>

<td>Description</td>

<td><label>

<textarea name="descr" id="descr"><?php echo $projectdetails['project_description']?></textarea>

</label></td>

</tr>

<tr>

<td>Status</td>

<td><label>

Showing the status of the project is easy, since its status can only be one of three things: overdue, completed, or pending.

{mospagebreak title=Determining the Value of the Variable}

One of the three values is contained in the $projectdetails['status'] variable. The only difficulty here is that you need to show the other two values in case the user wants to change from the current status. For example, say the status of the project is ‘pending.’ The user loads the edit_project page and that is the status that he should see on the form. However, the user must also be able to change that status to any of the other two states (overdue and completed).

So, we need to create a select box that will show the current status of the project as well as give the user the option to change the status. The logical steps to take in this case, are to first determine what value is in the $projectdetails['status'] variable, and then use the select box ‘selected’ attribute to select that value. I’ve used the switch() structure to do that work for me:

<select name="status" id="status">

<?php

$list=array(‘overdue’,’completed’,’pending’);


switch($projectdetails['status']){

 

case "overdue":

echo "<option value=’overdue’

selected";

 

break;

case "completed":

echo "<option value=’completed’

selected";

 

break;

case "pending":

echo "<option value=’pending’

selected";

 

break;

}


Once I’ve determined what the value of the variable is and selected it, I then run a for() loop and list all three values in the select box. You will see that I created an array called $list (highlighted in red above) that contains all three values:


for($x=0; $x < 4; $x++){

echo ">" .$list[$x]. "</option>";

}

?>

</select>

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

</label></td>

</tr>

<tr>

<td>Date Due </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>";

?>

<input type="hidden" name="createdt" value="<?php echo $projectdetails['create_dt']?>" />

</label></td>

</tr>

<tr>

<td>&nbsp;</td>

<td><label>

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

</label></td>

</tr>

</table>


</form>

<!– InstanceEndEditable –></td>

</tr>

<tr>

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

<tr>

<td><a href="edit_task.php?pid=<?php echo $projectdetails['pid'];?>">Change a Task in this Project</a> | <a href="edit_staff.php?pid=<?php echo $projectdetails['pid'];?>">Change Staff members of this Project </a> | <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 all there is to the edit project script. The next article will look at adding, editing, and viewing tasks for a particular project.

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

chat