A Simple PHP application
This little application has 5 main scripts
Main Page
Add Script
List Script
View Script
Changelog Script
See it in ACTION
The MAIN Page script
<?php /*
** Filename: index.php
** Coder: cthorn
** URL: http://PCCS-Linux.COM
**
**
**
*/
// Should this be a secure page
// if so, lets get the securepage source
// require('incs/securepage.php');
// Define the user and passwd for page
// $page_user=userid;
// $page_pw=password;
// Called the securePage() function
// securePage();
// Now start page
// Get variables and functions for application
require('appinclude.php');
// Create an array to pass to our navBar() function
$link_array = array (
"Main" => "index.php",
"List" => "list.php",
"Add" =>"add.php" );
// Create an array to pass to our array2select() function
$searchfield_str = array(
"wkorder_id" => "Workorder ID",
"closed" => "Status" );
// Create site header and define title for page
siteHeader("WorkOrder System");
// Create template pageTop and define heading...
pageTop("Demo WorkOrder System: MAIN MENU...");
// Insert a page break <P>
printPage();
// Creating a search form
print "<center>";
print "<H3>Search for: WorkOrder# or Status </H3>";
print "<FORM ACTION=search.php METHOD=post>";
print "<SELECT NAME=whichfield>";
array2select($searchfield_str);
print "</SELECT>";
print "<INPUT TYPE=text NAME=value>";
print "<INPUT TYPE=submit VALUE=\"Run Query\">";
print "</FORM>";
print "</center>";
// Create template pageBottom
pageBottom();
// Center the page navigation and pass $link_array from above
print "<center>"; navBar($link_array);
print "</center>";
print "<HR>";
// Create site footer...
siteFooter();
?>
The ADD data script
<?php // Include variables and functions
require('appinclude.php');
siteHeader("Web Technology SIG");
// Create an array for the navBar() function
$link_array = array (
"Main" => "index.php",
"List All" =>"list.php" );
// Create an array for the status options
$status_str = array(
" => ",
"closed" => "CLOSED" );
// Create a page break <P>
printPage();
// Create template using pageTop() function
pageTop("Demo WorkOrder System: ADD WorkOrder...");
// If this is a form submit process the data sent via post
if($submit) {
// Check to see if certian values where entered...
if((!$wkorder_id) || (!$problem_descrp)) {
print "Required Fields where not completed<br>";
print "WorkorderID and Problem Description but be entered";
print "<br>Click your back button to go back";
} else {
// Check the lenght of the wkorder_id must be 6 or more
$wkorderidlenght = strlen($wkorder_id);
if($wkorderidlenght < 6) {
print "Work Order ID not Correct";
print " it MUST be 6 digits or More";
exit;
}
// Prepare query for inserting data into table
$qry="INSERT into tracking VALUES('','$wkorder_id','$problem_descrp',
'$comments','$status')";
// $qry=addslashes($qry);
$res=mysql_query($qry);
// check to see if everything went okay
if(!$res) {
print "ERROR MySQL Said: " . mysql_error();
print $qry;
} else {
print "Record inserted<P>";
print "Enter a New record <A href=$PHP_SELF> here</a>";
}
}
} else {
// Create a FORM to get data
print "<CENTER>";
print "<FORM ACTION=\"$PHP_SELF\" METHOD=post>";
print "Workorder ID <INPUT TYPE=text NAME=wkorder_id><br>";
print "Problem Description<br>";
print "<TEXTAREA NAME=problem_descrp COLS=50 ROWS=5></TEXTAREA>";
print "<P>";
print "Comments<br>";
print "<TEXTAREA NAME=comments COLS=50 ROWS=5></TEXTAREA>";
print "<br>Status<br>";
print "<SELECT NAME=status>";
array2select($status_str);
print "</SELECT>";
print "<INPUT TYPE=submit NAME=submit VALUE=\"Enter Data\">";
print "</FORM>";
print "</CENTER>";
}
// Create template pageBottom
pageBottom();
// Center the page navigation and pass $link_array from above
print "<center>"; navBar($link_array, $page_dir);
print "</center>";
print "<HR>";
// Create site footer...
siteFooter();
?>
The LIST data script
<?php
require('appinclude.php');
$link_array = array (
"Main" => "index.php",
"Add" =>"add.php");
siteHeader("WorkOrder System");
pageTop("Demo WorkOrder System: LIST...");
printPage();
// Select all of the items in the tracking table
$result=mysql_query("SELECT * FROM tracking");
if(!$result) {
print "ERROR MysqlSaid: ". mysql_error();
} else {
print "<center>";
print "<table border=0 cellspacing=2 cellpadding=3>";
print "<tr><th>Changelog</th><th>View</th><th>WorkOrder #</th>";
print "<th>Problem</th><th>Status</th></tr>";
// Create an array of the results from the select stmt
while($row=mysql_fetch_array($result)) {
// Change the color of the status bgcolor
// you can change these
if($row[4]==") {
$color=red;
} else if($row[4]=="closed") {
$color=green;
}
// Generate HTML output to display results
print "<tr bgcolor=#CCCCCC>";
print "<td align=center valign=top>";
print "<A href=changelog.php?id=".$row[0]."&wkorder_id=".$row[1].">Log</a></td>";
print "<td valign=top><A href=view.php?id=".$row[0].">View</a></td>";
print "<td align=center valign=top>".$row[1]."</td>";
print "<td valign=top>".$row[2]."</td>";
print "<td align=center valign=top bgcolor=\"$color\">".$row[4]."</td>";
print "</tr>";
} // end of while statement
print "</table>";
print "</center>";
// if no data is available inform user
if(mysql_num_rows($result) == 0) {
print "<p><center>No Data to be displayed!...</center>";
}
}
pageBottom();
print "<center>"; navBar($link_array, $page_dir);
print "</center>";
print "<HR>"; siteFooter();
?>
The VIEW script
<?php require('appinclude.php');
$link_array = array (
"Main" => "index.php",
"List All" =>"list.php");
pageTop("WorkOrder System...");
printPage();
siteHeader("WorkOrder System");
$res=mysql_query("SELECT * FROM tracking WHERE id = '$id'");
if(!$res) {
print "ERROR MySQL Said: " . mysql_error();
} else {
$wkorder_id=htmlspecialchars(mysql_result($res,0,"wkorder_id"));
$problem=htmlspecialchars(mysql_result($res,0,"problem"));
$comments=htmlspecialchars(mysql_result($res,0,"comments"));
$status=htmlspecialchars(mysql_result($res,0,"status"));
}
$status_str = array(
" => ",
"closed" => "Closed"
);
print "<FORM>";
print "<H4>Workorder ID: " . $wkorder_id ."</H4>";
print "<H3>Problem Description</H3>\n";
print "<TEXTAREA NAME=problem_descrp COLS=50 ROWS=5>$problem</TEXTAREA>\n";
print "<P>";
print "Default Comments<br>";
print "<TEXTAREA NAME=comments COLS=50 ROWS=5>$comments</TEXTAREA>\n";
print "<br>Status :   $status <br>\n";
print "</FORM>";
$getchangelog = mysql_query("SELECT * FROM tracking_changelog WHERE
wkorder_id = '$wkorder_id'");
if(!$getchangelog) {
print "ERROR MySQL Said: ". mysql_error();
} else {
print "<H3 align=center>WorkOrder History</H3>";
print "<HR NOSHADE>";
print "<table border=0 cellspacing=2 cellpadding=2>";
print "<tr><th align=left>History</th><th>Time Entered</th></tr>";
while($changelog = mysql_fetch_array($getchangelog)) {
print "<tr><td valign=top>".$changelog[2]."</td>";
print "<td valign=top >".$changelog[3]."</td></tr>";
}
print "</table>";
}
pageBottom();
print "<center>"; navBar($link_array, $page_dir);
print "</center>";
print "<HR>"; siteFooter();
?>
The CHANGELOG script
<? require('appinclude.php');
$link_array = array (
"Main" => "index.php",
"Add" =>"add.php");
pageTop("Demo WorkOrder System: LIST...");
printPage();
siteHeader("WorkOrder System");
if($submit) {
$qry="INSERT into tracking_changelog VALUES('','$wkorder_id','$comments',now(),'$ip')";
// $qry=addslashes($qry);
$res=mysql_query($qry);
if(!$res) {
print "ERROR MySQL Said: " . mysql_error();
print $qry;
} else {
$result=mysql_query("SELECT status FROM tracking WHERE id = '$trackingid'");
if(!$result) {
print "ERROR: ".mysql_error();
} else {
$current_status=htmlspecialchars(mysql_result($result,0,"status"));
}
if($current_status != $this_status) {
$query = "UPDATE tracking SET status = '$this_status' WHERE wkorder_id = '$wkorder_id'";
$changestatus = mysql_query($query);
if(!$changestatus) {
print "Problem changing Status<br>";
print "ERROR: " . mysql_error();
exit;
} else {
}
} else {
// Do nothing it worked...
}
print "Record inserted<P>";
print "<A href=list.php>BACK to List</a>";
print "<P>";
// print "Status of $wkorder_id was changed";
if($this_status == ") {
$name = "WorkOrder System";
$mailto="cthorn@localhost";
$mailsubject="Form Reply from $name";
$mailbody .="Workorder #\t $wkorder_id\n";
$mailbody .="Comments:\n\n $comments\n";
$mailheaders="From: $name";
// mail($mailto, $mailsubject, $mailbody, $mailheaders);
print "<BR>$mailbody";
}
}
} else {
$res=mysql_query("SELECT * FROM tracking WHERE id = '$id'");
$status=htmlspecialchars(mysql_result($res,0,"status"));
$status_str = array(
" => ",
"closed" => "Closed"
);
print "<H3> Enter Changelog for Work Order# $wkorder_id</H3>";
print "<FORM ACTION=\"$PHP_SELF\" METHOD=post>\n";
print "<INPUT TYPE=hidden NAME=wkorder_id VALUE=$wkorder_id>\n";
print "<INPUT TYPE=hidden NAME=trackingid VALUE=$id>\n";
print "Comments<br>\n";
print "<TEXTAREA NAME=comments COLS=50 ROWS=5></TEXTAREA>\n";
print "<br>Status<br>\n";
print "<SELECT NAME=this_status>\n";
array2select($status_str,$status);
print "</SELECT>\n";
print "<INPUT TYPE=submit NAME=submit VALUE=\"Enter Data\">\n";
print "</FORM>\n";
}
pageBottom();
print "<center>"; navBar($link_array, $page_dir);
print "</center>";
print "<HR>"; siteFooter();
?>
|