Hello Chegg, TGIF we can always make it through a Friday. I need your help with
ID: 3902186 • Letter: H
Question
Hello Chegg, TGIF we can always make it through a Friday. I need your help with the following PHP task.
I’ve created a Guest Book PHP script that stores visitor’s names and e-mail addresses in a text file. I included functionality that allows users to view the guest book and prevents the same user name from being entered twice. I also, included code that sorts the guest book by name and deletes duplicate entries.
I have my code below, I've never been very good at commenting out code so Chegg, I need your assistance commenting out my code. If you anyone could help me out with this, I would be greatly. Appreciate any assistance you can give me. Thank you in advance for your help.
CODE BELOW
<!DOCTYPE html>
<html lang="en">
<head>
<title>Guest Book</title>
</head>
<body>
<h1>Justin's Class Guest Book</h1>
<?php
if ((file_exists("GuestBook.txt")) &&
(filesize("GuestBook.txt") != 0)) {
$GuestBookArray = file("GuestBook.txt");
}
else {
$GuestBookArray = array();
}
if (isset($_POST['submit'])) {
if (strlen($_POST['name'])==0) {
echo "You need to enter your name ";
}
else {
if (!in_array($_POST['name'],$GuestBookArray)) {
$GuestBookArray[] = $_POST['name'] . " ";
$GuestBookChanged = TRUE;
}
}
}
else if (isset($_GET['action'])) {
switch ($_GET['action']) {
case 'Delete Duplicates':
$GuestBookArray = array_unique(
$GuestBookArray);
$GuestBookArray = array_values(
$GuestBookArray);
$GuestBookChanged = TRUE;
break;
case 'Sort':
sort($GuestBookArray);
$GuestBookChanged = TRUE;
break;
} // End of the switch statement
}
if ($GuestBookChanged) {
if (count($GuestBookArray)>0) {
$NewMessages = implode($GuestBookArray);
$MessageStore = fopen("GuestBook.txt","wb");
if ($MessageStore === false)
echo "There was an error updating the message file ";
else {
fwrite($MessageStore,$NewMessages);
fclose($MessageStore);
}
}
else
unlink("GuestBook.txt");
}
if ((!file_exists("GuestBook.txt")) || (filesize("GuestBook.txt") == 0))
echo "<p>There are no entries in the guest book.</p> ";
else {
$GuestBookArray = file("GuestBook.txt");
echo "<table border="3" width="100%"> ";
$count = count($GuestBookArray);
foreach ($GuestBookArray as $Signer) {
echo "<tr> ";
echo "<td>" . htmlentities($Signer) . "</td> ";
echo "</tr> ";
}
echo "</table> ";
}
?>
<p>
<a href=
"GuestBook.php?action=Sort">
Sort Names</a><br />
<a href=
"GuestBook.php?action=Delete%20Duplicates">
Delete Duplicate Entries</a><br />
<form action="GuestBook.php" method="POST">
<strong>Sign the Guest Book:</strong><br />
<p>Your Name: <input type="text" name="name" /></p>
<P>Your Email:<input name="txtEmail" type="text" size="30" maxlength="50">
<p>
<input type="reset" name="reset" value="Clear Form" />
<input type="submit" name="submit" value="Sign Justin's Guest Book" />
</p>
</form>
</p>
</body>
</html>
Explanation / Answer
If you have any doubts, please give me comment...
<!DOCTYPE html>
<html lang="en">
<head>
<title>Guest Book</title>
</head>
<body>
<h1>Justin's Class Guest Book</h1>
<?php
//checking whether file exists or not
if ((file_exists("GuestBook.txt")) && (filesize("GuestBook.txt") != 0)) {
//if file exists opening file and get contents into array
$GuestBookArray = file("GuestBook.txt");
}
else {
//if file not exists declare new array
$GuestBookArray = array();
}
// check whether form submitted or not
if (isset($_POST['submit'])) {
// check whether user name entered or not
if (strlen($_POST['name'])==0) {
//displaying error message
echo "You need to enter your name ";
}
else {
//if user entered name, checking whether name already existed or not in array
if (!in_array($_POST['name'],$GuestBookArray)) {
// if not existsted push name into array
$GuestBookArray[] = $_POST['name'] . " ";
//set status to array data has been changed
$GuestBookChanged = TRUE;
}
}
}
//if form not submitted, checking whether any actions are given
else if (isset($_GET['action'])) {
// if we get any actions, then switch to that actions
switch ($_GET['action']) {
// action for remvoing duplicates
case 'Delete Duplicates':
//getting unique values from guest book array
$GuestBookArray = array_unique($GuestBookArray);
// getting value from object
$GuestBookArray = array_values($GuestBookArray);
//set status to array data has been changed
$GuestBookChanged = TRUE;
break;
case 'Sort':
// sorting Guestbook array
sort($GuestBookArray);
//set status to array data has been changed
$GuestBookChanged = TRUE;
break;
} // End of the switch statement
}
//if checking status whether changed or not
if ($GuestBookChanged) {
//checking whether array have values or not
if (count($GuestBookArray)>0) {
// concatenating array values rowwise
$NewMessages = implode($GuestBookArray);
// opening file to write contents into file
$MessageStore = fopen("GuestBook.txt","wb");
//checking whether data saved or not into file
if ($MessageStore === false)
// if not opened, displaying error message
echo "There was an error updating the message file ";
else {
//if file successfully opened then write into file
fwrite($MessageStore,$NewMessages);
//close the file
fclose($MessageStore);
}
}
else
//removing file
unlink("GuestBook.txt");
}
//checking whether existed or not, if exists checking any rows are present or not
if ((!file_exists("GuestBook.txt")) || (filesize("GuestBook.txt") == 0))
echo "<p>There are no entries in the guest book.</p> ";
else {
//storing data into array
$GuestBookArray = file("GuestBook.txt");
//displaying data in tabular format
echo "<table border="3" width="100%"> ";
//getting how many rows are in array
$count = count($GuestBookArray);
//displaying data from array using foreach loop
foreach ($GuestBookArray as $Signer) {
echo "<tr> ";
echo "<td>" . htmlentities($Signer) . "</td> ";
echo "</tr> ";
}
echo "</table> ";
}
?>
<!-- Displaying form or actions to user -->
<p><a href="GuestBook.php?action=Sort">Sort Names</a><br />
<a href="GuestBook.php?action=Delete%20Duplicates">Delete Duplicate Entries</a><br />
<form action="GuestBook.php" method="POST">
<strong>Sign the Guest Book:</strong><br />
<p>Your Name: <input type="text" name="name" /></p>
<P>Your Email:<input name="txtEmail" type="text" size="30" maxlength="50">
<p>
<input type="reset" name="reset" value="Clear Form" />
<input type="submit" name="submit" value="Sign Justin's Guest Book" />
</p>
</form>
</p>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.