I\'m working with PHP forms and writing and reading to files. I have to a \"prog
ID: 3591934 • Letter: I
Question
I'm working with PHP forms and writing and reading to files.
I have to a "program" that accepts bug reports. Step 1 was to create a form and display list for the submitted data. Step 2 was to validate the data, display the output, and write the output to a text file. Step 3 is to create an actual report with the ability to update it the data. Step 4 is to display the update. I am working on steps 3 & 4.
Step 3 (see "Bug Reports" image below) is done. My data is being read and displayed from my text file and and update option works. However, I need to write the program so that multiple updates can be submitted and displayed overtime. Right now, if you add a new update it overwrites the original. So the second image should actually have an "Update 1: first update" row followed by future updates "Update 2: second update". I know this goes into arrays but the text doesn't really have a good example and I haven't been able to find one online. My code for both files is below.
/////Step 3 code///////
<?PHP
//STARTUP AND INITIALIZATION SECTION
// ini_set('display_errors', 'On');
// error_reporting(E_ALL);
$Author = "Jennifer Bailey";
$LabID = "CTP130-Programming Lab 6";
$Subject = "Working with Files and Directories"; // title of the lab. For Lab02 that would be "Data Types, Operators, and Expressions"
$Title = $Author.", ".$LabID; // Create a PHP variable called $Title composed of the Author (you) and LabID and the LabID
// Now lets create a niffty little variable containing today's date and time!
$rightNow = ""; // Initialize a string variable called $rightNow
$tz = 'America/New_York'; // ser the rz variable to the Time zone for our area. See http://php.net/manual/en/timezones.php and click on "America"
$timestamp = time(); // a special internal number for the moment the timestamp was create. Nothing magic about the variable name.
$dt = new DateTime("now", new DateTimeZone($tz)); //Create a $dt object from the class DateTime. first argument "must" be a string. "now" means the
// the date and time in the US East coast.
$dt->setTimestamp($timestamp); //adjust the dt object to correct timestamp
$rightNow = $dt->format('H:i:s'); // Store the formatted time of the $dt object in a string variable $rightNow
//End of Niffty
//Includes Go here
//End of all the INCLUDES
//Initialize your variables, including calling functions
//END OF STARTUP AND INITIALIZATION SECTION
?>
<!-- FOR **NOW** your HTML with any embedded PHP goes next -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title><?PHP echo $Title; ?></title> <!-- The PHP variable for Title -->
</head>
<body>
<!-- A HEADER LATER this will migrate into an include file -->
<table width="100%">
<tr>
<td><h1><?PHP echo $Subject; ?></h1></td> <!-- Put some kind of title. into the first column of row 1. Usually it is good form that the <title>
and any kind of internal page heading are either the same or have a relationship -->
<td><strong><?PHP echo $Title; ?></strong></td>
</tr>
</table>
<hr />
<!-- End of HEADER -->
<!-- Define an HTML <DIV> area to hold all your stuff you do -->
<h2>Bug Reports</h2>
<p> Return to <a href="bug_List.php">bug list</a>.</p>
<?php
//display the report
$Dir = "submissions";
if (is_dir($Dir)) {
$BugReports = scandir($Dir);
foreach ($BugReports as $FileName) {
if (($FileName != ".") && ($FileName != "..")) {
$Report = file($Dir . "/" . $FileName);
echo "<table border='1' width='250px'>";
echo '<tr>';
echo '<td><p>Bug Description</p></td>';
echo '<td>'.htmlentities($Report[0]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Name</p></td>';
echo '<td>'.htmlentities($Report[1]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Version</p></td>';
echo '<td>'.htmlentities($Report[2]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Type of Hardware</p></td>';
echo '<td>'.htmlentities($Report[3]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Operating System</p></td>';
echo '<td>'.htmlentities($Report[4]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Frequency of Occurrence</p></td>';
echo '<td>'.htmlentities($Report[5]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Proposed Solution</p></td>';
echo '<td>'.htmlentities($Report[6]).'</td>';
echo '</tr>';
echo '</table>';
//add an update
echo "<h3>Add an update</h3>";
echo '<form name="Update Bug Report" action="BugReportUpdate.php" method="post">';
echo '<p><input type="text" name="update"/></p>';
echo '<p><input type="submit" name="submit" value="Update the Report" /></p>';
echo "</form>";
echo "<hr /> ";
}
}
}
?>
<!-- END of the DIV and Textarea used to hold information -->
<!-- LETS DO A Simple FOOTER -->
<hr/>
<table width="100%">
<tr>
<td><strong>As of</strong> <?PHP echo date('l jS of F Y')." at ".$dt->format('H:i:s'); ?></td>
<td>© <?PHP echo date('Y');?> by AACC <?PHP echo $Title ?>.</td>
</tr>
<tr>
<td><a href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0!" height="31" width="88" /></a>
</td>
<td>All Rights Reserved.</td>
</tr>
</table>
</body>
</html>
/////Step 4////
<?PHP
//STARTUP AND INITIALIZATION SECTION
// ini_set('display_errors', 'On');
// error_reporting(E_ALL);
$Author = "Jennifer Bailey";
$LabID = "CTP130-Programming Lab 6";
$Subject = "Working with Files and Directories"; // title of the lab. For Lab02 that would be "Data Types, Operators, and Expressions"
$Title = $Author.", ".$LabID; // Create a PHP variable called $Title composed of the Author (you) and LabID and the LabID
// Now lets create a niffty little variable containing today's date and time!
$rightNow = ""; // Initialize a string variable called $rightNow
$tz = 'America/New_York'; // ser the rz variable to the Time zone for our area. See http://php.net/manual/en/timezones.php and click on "America"
$timestamp = time(); // a special internal number for the moment the timestamp was create. Nothing magic about the variable name.
$dt = new DateTime("now", new DateTimeZone($tz)); //Create a $dt object from the class DateTime. first argument "must" be a string. "now" means the
// the date and time in the US East coast.
$dt->setTimestamp($timestamp); //adjust the dt object to correct timestamp
$rightNow = $dt->format('H:i:s'); // Store the formatted time of the $dt object in a string variable $rightNow
//End of Niffty
//Includes Go here
//End of all the INCLUDES
//Initialize your variables, including calling functions
//END OF STARTUP AND INITIALIZATION SECTION
?>
<!-- FOR **NOW** your HTML with any embedded PHP goes next -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title><?PHP echo $Title; ?></title> <!-- The PHP variable for Title -->
</head>
<body>
<!-- A HEADER LATER this will migrate into an include file -->
<table width="100%">
<tr>
<td><h1><?PHP echo $Subject; ?></h1></td> <!-- Put some kind of title. into the first column of row 1. Usually it is good form that the <title>
and any kind of internal page heading are either the same or have a relationship -->
<td><strong><?PHP echo $Title; ?></strong></td>
</tr>
</table>
<hr />
<!-- End of HEADER -->
<!-- Define an HTML <DIV> area to hold all your stuff you do -->
<h2>Bug Reports Update</h2>
<?php
//assign input
$Update = $_POST['update'];
//display updated report
$Dir = "submissions";
if (is_dir($Dir)) {
$BugReports = scandir($Dir);
foreach ($BugReports as $FileName) {
if (($FileName != ".") && ($FileName != "..")) {
$Report = file($Dir . "/" . $FileName);
echo "<table border='1' width='250px'>";
echo '<tr>';
echo '<td><p>Bug Description</p></td>';
echo '<td>'.htmlentities($Report[0]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Name</p></td>';
echo '<td>'.htmlentities($Report[1]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Version</p></td>';
echo '<td>'.htmlentities($Report[2]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Type of Hardware</p></td>';
echo '<td>'.htmlentities($Report[3]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Operating System</p></td>';
echo '<td>'.htmlentities($Report[4]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Frequency of Occurrence</p></td>';
echo '<td>'.htmlentities($Report[5]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Proposed Solution</p></td>';
echo '<td>'.htmlentities($Report[6]).'</td>';
echo '</tr>';
echo '<tr>';
//display updated data
if(empty($Update) == FALSE) {
echo '<td><p>Update</p></td>';
echo '<td>'.$Update.'</td>';
} else {
echo '<td><p>There are no updates.</p></td>';
}
echo '</tr>';
echo '</table>';
echo '<p> Return to <a href="bug_List.php">bug list</a>, OR add another <a href="BugReport.php">update</a>.</p>';
echo "<hr /> ";
}
}
}
?>
<!-- END of the DIV and Textarea used to hold information -->
<!-- LETS DO A Simple FOOTER -->
<hr/>
<table width="100%">
<tr>
<td><strong>As of</strong> <?PHP echo date('l jS of F Y')." at ".$dt->format('H:i:s'); ?></td>
<td>© <?PHP echo date('Y');?> by AACC <?PHP echo $Title ?>.</td>
</tr>
<tr>
<td><a href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0!" height="31" width="88" /></a>
</td>
<td>All Rights Reserved.</td>
</tr>
</table>
</body>
</html>
Explanation / Answer
/STARTUP AND INITIALIZATION SECTION
// ini_set('display_errors', 'On');
// error_reporting(E_ALL);
$Author = "Jennifer Bailey";
$LabID = "CTP130-Programming Lab 6";
$Subject = "Working with Files and Directories"; // title of the lab. For Lab02 that would be "Data Types, Operators, and Expressions"
$Title = $Author.", ".$LabID; // Create a PHP variable called $Title composed of the Author (you) and LabID and the LabID
// Now lets create a niffty little variable containing today's date and time!
$rightNow = ""; // Initialize a string variable called $rightNow
$tz = 'America/New_York'; // ser the rz variable to the Time zone for our area. See http://php.net/manual/en/timezones.php and click on "America"
$timestamp = time(); // a special internal number for the moment the timestamp was create. Nothing magic about the variable name.
$dt = new DateTime("now", new DateTimeZone($tz)); //Create a $dt object from the class DateTime. first argument "must" be a string. "now" means the
// the date and time in the US East coast.
$dt->setTimestamp($timestamp); //adjust the dt object to correct timestamp
$rightNow = $dt->format('H:i:s'); // Store the formatted time of the $dt object in a string variable $rightNow
//End of Niffty
//Includes Go here
//End of all the INCLUDES
//Initialize your variables, including calling functions
//END OF STARTUP AND INITIALIZATION SECTION
?>
<!-- FOR **NOW** your HTML with any embedded PHP goes next -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title><?PHP echo $Title; ?></title> <!-- The PHP variable for Title -->
</head>
<body>
<!-- A HEADER LATER this will migrate into an include file -->
<table width="100%">
<tr>
<td><h1><?PHP echo $Subject; ?></h1></td> <!-- Put some kind of title. into the first column of row 1. Usually it is good form that the <title>
and any kind of internal page heading are either the same or have a relationship -->
<td><strong><?PHP echo $Title; ?></strong></td>
</tr>
</table>
<hr />
<!-- End of HEADER -->
<!-- Define an HTML <DIV> area to hold all your stuff you do -->
<h2>Bug Reports</h2>
<p> Return to <a href="bug_List.php">bug list</a>.</p>
<?php
//display the report
$Dir = "submissions";
if (is_dir($Dir)) {
$BugReports = scandir($Dir);
foreach ($BugReports as $FileName) {
if (($FileName != ".") && ($FileName != "..")) {
$Report = file($Dir . "/" . $FileName);
echo "<table border='1' width='250px'>";
echo '<tr>';
echo '<td><p>Bug Description</p></td>';
echo '<td>'.htmlentities($Report[0]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Name</p></td>';
echo '<td>'.htmlentities($Report[1]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Version</p></td>';
echo '<td>'.htmlentities($Report[2]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Type of Hardware</p></td>';
echo '<td>'.htmlentities($Report[3]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Operating System</p></td>';
echo '<td>'.htmlentities($Report[4]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Frequency of Occurrence</p></td>';
echo '<td>'.htmlentities($Report[5]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Proposed Solution</p></td>';
echo '<td>'.htmlentities($Report[6]).'</td>';
echo '</tr>';
echo '</table>';
//add an update
echo "<h3>Add an update</h3>";
echo '<form name="Update Bug Report" action="BugReportUpdate.php" method="post">';
echo '<p><input type="text" name="update"/></p>';
echo '<p><input type="submit" name="submit" value="Update the Report" /></p>';
echo "</form>";
echo "<hr /> ";
}
}
}
?>
<!-- END of the DIV and Textarea used to hold information -->
<!-- LETS DO A Simple FOOTER -->
<hr/>
<table width="100%">
<tr>
<td><strong>As of</strong> <?PHP echo date('l jS of F Y')." at ".$dt->format('H:i:s'); ?></td>
<td>© <?PHP echo date('Y');?> by AACC <?PHP echo $Title ?>.</td>
</tr>
<tr>
<td><a href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0!" height="31" width="88" /></a>
</td>
<td>All Rights Reserved.</td>
</tr>
</table>
</body>
</html>
/////Step 4////
<?PHP
//STARTUP AND INITIALIZATION SECTION
// ini_set('display_errors', 'On');
// error_reporting(E_ALL);
$Author = "Jennifer Bailey";
$LabID = "CTP130-Programming Lab 6";
$Subject = "Working with Files and Directories"; // title of the lab. For Lab02 that would be "Data Types, Operators, and Expressions"
$Title = $Author.", ".$LabID; // Create a PHP variable called $Title composed of the Author (you) and LabID and the LabID
// Now lets create a niffty little variable containing today's date and time!
$rightNow = ""; // Initialize a string variable called $rightNow
$tz = 'America/New_York'; // ser the rz variable to the Time zone for our area. See http://php.net/manual/en/timezones.php and click on "America"
$timestamp = time(); // a special internal number for the moment the timestamp was create. Nothing magic about the variable name.
$dt = new DateTime("now", new DateTimeZone($tz)); //Create a $dt object from the class DateTime. first argument "must" be a string. "now" means the
// the date and time in the US East coast.
$dt->setTimestamp($timestamp); //adjust the dt object to correct timestamp
$rightNow = $dt->format('H:i:s'); // Store the formatted time of the $dt object in a string variable $rightNow
//End of Niffty
//Includes Go here
//End of all the INCLUDES
//Initialize your variables, including calling functions
//END OF STARTUP AND INITIALIZATION SECTION
?>
<!-- FOR **NOW** your HTML with any embedded PHP goes next -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1"/>
<title><?PHP echo $Title; ?></title> <!-- The PHP variable for Title -->
</head>
<body>
<!-- A HEADER LATER this will migrate into an include file -->
<table width="100%">
<tr>
<td><h1><?PHP echo $Subject; ?></h1></td> <!-- Put some kind of title. into the first column of row 1. Usually it is good form that the <title>
and any kind of internal page heading are either the same or have a relationship -->
<td><strong><?PHP echo $Title; ?></strong></td>
</tr>
</table>
<hr />
<!-- End of HEADER -->
<!-- Define an HTML <DIV> area to hold all your stuff you do -->
<h2>Bug Reports Update</h2>
<?php
//assign input
$Update = $_POST['update'];
//display updated report
$Dir = "submissions";
if (is_dir($Dir)) {
$BugReports = scandir($Dir);
foreach ($BugReports as $FileName) {
if (($FileName != ".") && ($FileName != "..")) {
$Report = file($Dir . "/" . $FileName);
echo "<table border='1' width='250px'>";
echo '<tr>';
echo '<td><p>Bug Description</p></td>';
echo '<td>'.htmlentities($Report[0]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Name</p></td>';
echo '<td>'.htmlentities($Report[1]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Product Version</p></td>';
echo '<td>'.htmlentities($Report[2]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Type of Hardware</p></td>';
echo '<td>'.htmlentities($Report[3]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Operating System</p></td>';
echo '<td>'.htmlentities($Report[4]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Frequency of Occurrence</p></td>';
echo '<td>'.htmlentities($Report[5]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td><p>Proposed Solution</p></td>';
echo '<td>'.htmlentities($Report[6]).'</td>';
echo '</tr>';
echo '<tr>';
//display updated data
if(empty($Update) == FALSE) {
echo '<td><p>Update</p></td>';
echo '<td>'.$Update.'</td>';
} else {
echo '<td><p>There are no updates.</p></td>';
}
echo '</tr>';
echo '</table>';
echo '<p> Return to <a href="bug_List.php">bug list</a>, OR add another <a href="BugReport.php">update</a>.</p>';
echo "<hr /> ";
}
}
}
?>
<!-- END of the DIV and Textarea used to hold information -->
<!-- LETS DO A Simple FOOTER -->
<hr/>
<table width="100%">
<tr>
<td><strong>As of</strong> <?PHP echo date('l jS of F Y')." at ".$dt->format('H:i:s'); ?></td>
<td>© <?PHP echo date('Y');?> by AACC <?PHP echo $Title ?>.</td>
</tr>
<tr>
<td><a href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0!" height="31" width="88" /></a>
</td>
<td>All Rights Reserved.</td>
</tr>
</table>
</body>
</html>