In: Computer Science
PHP
A local client needs to take his static web page for ordering organic vegetables and build a dynamic page. He doesn't have much money to spend, so we are going to write orders to a flat file. He is also going to need a page that he can go to that will display his orders.
He has already met with Sharron, so we've got a specification already assembled. Here are the details:
The customer will go to a form page where they can enter an order for vegetables. The file will be called orderVegetables.php. Here are products a customer can order:
The customer should be able choose a quantity of items beside any of the vegetable options. Only allow orders where there is at least one thing ordered. Also, if the total price is over $50 then they qualify for free delivery, otherwise delivery cost is $5.00. (You can apply the delivery fee after tax, or before). Be sure to also capture the customers name, email and phone number on the form so the client can contact them to setup delivery. Make sure each order has the date and time of the order as well. Don't forget to use \r\n for a new line at the end of each record.
When the user clicks 'submit,' a POST request is made to processVeggies.php where the order is written to the file veggie-orders.txt. The text file should be simply a storage place in lieu of using a proper database. Some would call it a 'flat-file' storage system. Make sure to lock the file before appending, and to unlock it afterwards. Display a thank you message to the client and print their order details to the screen. Let them know that they will be contacted within the next business day.
Of course the client would like a page to view all the orders. This file will be called viewOrders.php. The client would like a link on this page to reset the orders (reset the veggie-orders.txt file) called Reset Orders. This link will call a file resetOrders.php. The client will typically print off a days worth of orders and then reset the form for the next day. Don't worry about locking the file on your reset page.
Good luck! We need this prototype soon!
- Ima Plant
Senior Consultant,
Acme International Inc.
====
The order form also needs to capture customer name, phone number, and email.
What to expects
1. The input page (named orderVegetables.php) needs to have the following fields:
a. Select drop-down fields should be used to allow users to choose a quantity for each item. Limit the options to 0-30.
b.We also need fields for name, email, and phone number.
2. Look and Feel: Use bootstrap or not, your choice. The header of each page should contain a masthead/header image that would befit an upscale veggie shop. Please use GIMP, MS PAINT or some other editor to create this header image for the top of your page. Or better yet, set your header text using <h1> or <h2> and set the image you create as a background-image in your CSS. As well, all formatting should be done using CSS in an external stylesheet. Please use a div with an id of 'container' to hold your content. Ensure that this simple theme is present for ALL of your pages!
3. The processing page (named processVeggies.php) needs to include the following:
a. Order details gathered from input form.
b. Whether or not delivery is free - display a message to the user one way or the other (include dollar sign and two decimal places - that is, format for currency)
c. Assume a provincial tax rate of 15%
d. Calculate and display the details of their order, net amount before tax, tax amount, delivery amount, and total amount.
e. Include the time, date, and year (for Atlantic Canada) that the order was processed.
f. Ensure that there is some type of validation on the processing page. If someone tried to run it without accessing the form page first, it should give the user a link back to the order page.
4. The processing page (processVeggies.php) should also write this order to a file.
a .Ensure file is accessible to your script, but hidden from public browsing. Please note that veggie-orders.txt MUST be stored one directory behind your publishing directory. "$DOCUMENT_ROOT/../veggie-orders.txt" would be an example.
b. Orders file should be delimited by a "\t" or some other delimiter. " \r\n" at the end of each line.
5. The viewOrders.php page should open the file for reading only and display, in html, all of the pending orders for the day. The look and feel of this page should follow the store's theme. Though this page would be used only by administrators of the site, we will not lock it down. For now, assume it will be live for all to view. Please create a table to hold all of the pending orders. Each line of the order file could be a table row. You can keep this simple, no need to use explode() or list() if you don't want to. As well on this page, create a hyperlink to resetOrders.php (see below). If there are no pending orders, ensure there is a message to the user stating the fact.
6. Create another page called resetOrders.php. This page should reset the orders file (delete the file contents), give the user confirmation that the file has been cleared and give them a link/instructions on what to do next.
7. All pages should be accessible via links. So there should be some sort of menu. I DON'T need a link to process orders as that would be silly. Remember menus should go at the top of your pages.
8.Comments, Code Formatting, Submission Packaging (variable naming, page naming).
header.php
<!DOCTYPE html>
<html>
<title>Vegetable App </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1">
<link rel="stylesheet"
href="https://www.w3schools.com/w3css/4/w3.css">
<style>
body {font-family: "Times New Roman", Georgia, Serif;}
h1, h2, h3, h4, h5, h6 {
font-family: "Playfair Display";
letter-spacing: 5px;
}
</style>
<body>
<div class="w3-top">
<div class="w3-bar w3-white w3-padding w3-card"
style="letter-spacing:4px;">
<a href="#home" class="w3-bar-item w3-button">Vegetable
App</a>
<div class="w3-right w3-hide-small">
<a href="orderVegetables.php" class="w3-bar-item
w3-button">Order Vegetables</a>
<a href="viewOrders.php" class="w3-bar-item w3-button">View
Orders</a>
</div>
</div>
</div>
footer.php
<!-- Footer -->
<footer class="w3-center w3-light-grey w3-padding-32">
</footer>
</body>
</html>
index.php
<?php require_once 'header.php'; ?>
<!-- Contact Section -->
<h1 style="margin: 2em">Welcome to Vegetable Ordering App
</h1>
<!-- End page content -->
</div>
<?php require_once 'footer.php'; ?>
orderVegetables.php
<?php require_once 'header.php'; ?>
<!-- Contact Section -->
<?php
$option = "";
$err_msg = "";
for ($i = 0; $i < 30; $i++) {
$option .= "<option value='$i'>$i</option>";
}
$msg = "";
$error = 0;
if (!empty($_POST)) {
$potatoes = doubleval($_POST["potatoes"]) * 6.00;
$carrots = doubleval($_POST["carrots"]) * 6.00;
$coli = doubleval($_POST["coli"]) * 6.00;
$total = $potatoes + $carrots + $coli;
if ($total <= 0) {
$error = 1;
$err_msg = "Select Atleast One Order";
}
if (!$error) {
$insert = $_POST["name"] . "," . $_POST["email"] . "," .
$_POST["number"] . "," . $_POST["potatoes"] . "," .
$_POST["carrots"] . "," . $_POST["coli"] . "," . date("Y/m/d") .
"," . date("h:i:sa") . "\r\n";
file_put_contents(dirname(__FILE__) . "/../veggie-orders.txt",
$insert, FILE_APPEND | LOCK_EX);
$delivery = ($total > 50) ? 0 : 5;
$msg = "Net Amount= $ $total, Tax Amount= $ " . (15 * $total / 100)
. ",Delivery $ $delivery"
. ", Total Amount is $ " . ($total + (15 * $total / 100) +
$delivery);
}
}
?>
<div class="w3-container w3-padding-64" id="contact">
<?php
if (empty($_POST) || !empty($err_msg)) {
echo "<p>$err_msg</p>";
?>
<h1>Order Vegetable</h1><br>
<form action="" target="_blank" method="post">
<p><input class="w3-input w3-padding-16" type="text"
placeholder="Name" required name="name"></p>
<p><input class="w3-input w3-padding-16" type="email"
placeholder="Email" required name="email"></p>
<p><input class="w3-input w3-padding-16" type="text"
placeholder="Number" required name="number"></p>
<p><label>5 lb Potatoes, $6.00
each</label><select class="w3-input w3-padding-16"
required name="potatoes">
<?php echo $option ?>
</select></p>
<p><label>3 lb Carrots, $3.75
each</label><select class="w3-input w3-padding-16"
required name="carrots">
<?php echo $option ?>
</select></p>
<p><label>1 Cauliflower, $4.00
each</label><select class="w3-input w3-padding-16"
required name="coli">
<?php echo $option ?>
</select></p>
<p><button class="w3-button w3-light-grey w3-section"
type="submit">ORDER</button></p>
</form>
<?php
} else {
echo "<h1 style='margin: 2em'>$msg</h1>";
}
?>
</div>
<!-- End page content -->
</div>
<?php require_once 'footer.php'; ?>
viewOrders.php
<?php require_once 'header.php'; ?>
<!-- Contact Section -->
<div class="w3-container w3-padding-64" id="contact">
<h1>Orders</h1><br>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Number</th>
<th>Potatoes</th>
<th>Carrots</th>
<th>Cauliflower</th>
<th>Date</th>
<th>Time</th>
</tr>
<?php
$conetents = explode("\r\n", file_get_contents(dirname(__FILE__) .
"/../veggie-orders.txt"));
foreach ($conetents as $val) {
$arr = explode(",", $val);
if (count($arr) > 1) {
if (strtotime($arr[6]) == strtotime(date("Y/m/d")))
echo"<tr>";
echo"<td>$arr[0]</td>";
echo"<td>$arr[1]</td>";
echo"<td>$arr[2]</td>";
echo"<td>$arr[3]</td>";
echo"<td>$arr[4]</td>";
echo"<td>$arr[5]</td>";
echo"<td>$arr[6]</td>";
echo"<td>$arr[7]</td>";
echo"</tr>";
}
}
?>
</table>
<a href="resetOrders.php">Reset Orders</a>
</div>
<!-- End page content -->
</div>
<?php require_once 'footer.php'; ?>
resetOrders.php
<?php require_once 'header.php'; ?>
<!-- Contact Section -->
<div class="w3-container w3-padding-64" id="contact">
<?php
if (!empty(file_get_contents(dirname(__FILE__) .
"/../veggie-orders.txt"))) {
file_put_contents(dirname(__FILE__) . "/../veggie-orders.txt",
"");
echo"<h1 style='margin: 2em'>All Orders are reset
</h1>";
} else {
echo"<h1 style='margin: 2em'>There are no pending orders
</h1>";
}
?>
</div>
<!-- End page content -->
</div>
<?php require_once 'footer.php'; ?>