modify that php script file as below: Add one additional column to the shopping
ID: 3910544 • Letter: M
Question
modify that php script file as below:
Add one additional column to the shopping cart. The column is named "Total Each Item" that will show the total cost of each kind of fruits in the cart.
Add one row at the end of the shopping cart that will show the total cost for all kinds of fruits in the cart.
The code of the shopping cart (carttest_3.php) was listed below:
<?php
session_start();
print"
<!DOCTYPE html>
<html>
<head>
<title>shopping cart</title>
<style type = "text/css" media = "all">
body{ background-color: linen;}
td, h1{color: maroon; text-align:center;}
</style>
</head>
<body>
<form action="carttest_3.php" method="post">
<table border="0">
<tr>
<th colspan="6"><h1>Item Available</h1></th>
</tr>
<tr>
<th>product id</th>
<th>prorduct name</th>
<th>description</th>
<th>Price($)</th>
<th>last updated</th>
<th>Quantity</th>
</tr>
";
//-------connect to MySQL database system------------------------------------
$dbname="csc395_6";
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){ print "Could not connect: " . mysqli_error($conn);}
//-------choose a database to use------------------------------------
mysqli_select_db($conn, $dbname);
//--------retrive data from the database---------------------------------------
$sql = "SELECT * FROM products";
$retval = mysqli_query($conn, $sql);
if (!$retval){ print "Error retrieving data from a table: " . mysqli_error($conn); }
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
$tmp=$row['p_name'];
$products["$tmp"]=$row;
}
mysqli_close($conn);
//--------------------------create product lists---------------------------------------------------
foreach($products as $p=>$a)
{
print "<tr>";
foreach($a as $k=>$v)
{
print "<td>$v</td>";
}
print "<td><input type="text" name="$p" size="5"></td></tr>";
}
print"
<tr><td></td><td></td>
<td><input type="submit" value="add to cart"></td>
<td></td><td></td><td></td>
</table>
</form>
<br>
";
//------------------------shopping cart-----------------------------------------------------
foreach($products as $p=>$v)
{
if(isset($_POST["$p"]))
{
if(is_numeric($_POST["$p"]))
{
if(isset($_SESSION['cart']["$p"]))
{
//The shopping cart already has a specific kind of fruit, but now add additional
$_SESSION['cart']["$p"]=$_SESSION['cart']["$p"]+$_POST["$p"];
}
else
{ //The shopping cart has no a specific kind of fruit, but now add some
$_SESSION['cart']["$p"]=$_POST["$p"];
}
}
elseif($_POST["$p"]=="Remove")
{
//remove the selected products from the shopping cart
unset($_SESSION['cart']["$p"]);
}
}
}
print"
<fieldset>
<legend>Your Shopping Cart</legend>";
if(!isset($_SESSION['cart']))
{
$_SESSION['cart']=array();
print "Your shopping cart is empty";
}
else
{
print "
<form action ="carttest_3.php" method="post">
<table border="0">
<tr>
<th>Item</th>
<th>Quantity</th>
<th></th>
</tr>";
foreach($_SESSION['cart'] as $key=>$value)
{
print "
<tr>
<td>$key</td>
<td>$value</td>
<td><input type="submit" name="$key" value="Remove"></td>
</tr>";
}
print "</table></form>";
}
print"
</fieldset>
</body>
</html>";
?>
The code you need to create the database and sample data:
CREATE DATABASE csc395_6;
CREATE TABLE products(
p_id INT UNSIGNED NOT NULL PRIMARY KEY,
p_name VARCHAR(40) NOT NULL,
p_description VARCHAR(200) NOT NULL,
p_price INT NOT NULL,
add_date DATETIME NOT NULL
);
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(1, "grape","green grape", 9, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(2, "berry","red berry", 8, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(3, "cherry","red cherry", 7, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(4, "crabapple","red crabapple", 6, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(5, "cantaloupe","sweet cantaloupe", 5, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(6, "apple","green apple", 4, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(7, "orange","green apple", 4, NOW());
<?php
session_start();
print"
<!DOCTYPE html>
<html>
<head>
<title>shopping cart</title>
<style type = "text/css" media = "all">
body{ background-color: linen;}
td, h1{color: maroon; text-align:center;}
</style>
</head>
<body>
<form action="carttest_3.php" method="post">
<table border="0">
<tr>
<th colspan="6"><h1>Item Available</h1></th>
</tr>
<tr>
<th>product id</th>
<th>prorduct name</th>
<th>description</th>
<th>Price($)</th>
<th>last updated</th>
<th>Quantity</th>
</tr>
";
//-------connect to MySQL database system------------------------------------
$dbname="csc395_6";
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){ print "Could not connect: " . mysqli_error($conn);}
//-------choose a database to use------------------------------------
mysqli_select_db($conn, $dbname);
//--------retrive data from the database---------------------------------------
$sql = "SELECT * FROM products";
$retval = mysqli_query($conn, $sql);
if (!$retval){ print "Error retrieving data from a table: " . mysqli_error($conn); }
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
$tmp=$row['p_name'];
$products["$tmp"]=$row;
}
mysqli_close($conn);
//--------------------------create product lists---------------------------------------------------
foreach($products as $p=>$a)
{
print "<tr>";
foreach($a as $k=>$v)
{
print "<td>$v</td>";
}
print "<td><input type="text" name="$p" size="5"></td></tr>";
}
print"
<tr><td></td><td></td>
<td><input type="submit" value="add to cart"></td>
<td></td><td></td><td></td>
</table>
</form>
<br>
";
//------------------------shopping cart-----------------------------------------------------
foreach($products as $p=>$v)
{
if(isset($_POST["$p"]))
{
if(is_numeric($_POST["$p"]))
{
if(isset($_SESSION['cart']["$p"]))
{
//The shopping cart already has a specific kind of fruit, but now add additional
$_SESSION['cart']["$p"]=$_SESSION['cart']["$p"]+$_POST["$p"];
}
else
{ //The shopping cart has no a specific kind of fruit, but now add some
$_SESSION['cart']["$p"]=$_POST["$p"];
}
}
elseif($_POST["$p"]=="Remove")
{
//remove the selected products from the shopping cart
unset($_SESSION['cart']["$p"]);
}
}
}
print"
<fieldset >
<legend>Your Shopping Cart</legend>";
if(!isset($_SESSION['cart']))
{
$_SESSION['cart']=array();
print "Your shopping cart is empty";
}
else
{
print "
<form action ="carttest_3.php" method="post">
<table border="0">
<tr>
<th>Item</th>
<th>Quantity</th>
<th></th>
</tr>";
foreach($_SESSION['cart'] as $key=>$value)
{
print "
<tr>
<td>$key</td>
<td>$value</td>
<td><input type="submit" name="$key" value="Remove"></td>
</tr>";
}
print "</table></form>";
}
print"
</fieldset>
</body>
</html>";
?>
+C 127.0.0. carttest_4.php Item Available product id prorduct name description Pice) last updated Quantity 9 2018-06-26 16:51:04 8 2018-06-26 16:51:10 7 2018-06-26 16:51:15 crabapple red crabapple 6 2018-06-26 16:51:21 cantaloupe sweet cantaloupe 5 2018-06-26 16:51:26 4 2018-06-26 16:51:31 4 2018-06-26 16:51:37 grape berry cherry green grape red berry red cherry green apple green apple add to cart apple orange Your Shopping Cart Item Quantity Total Each Item grape 1 apple Remove Remove Total 17Explanation / Answer
<?php
session_start();
print"
<!DOCTYPE html>
<html>
<head>
<title>shopping cart</title>
<style type = "text/css" media = "all">
body{ background-color: linen;}
td, h1{color: maroon; text-align:center;}
</style>
</head>
<body>
<form action="carttest_3.php" method="post">
<table border="0">
<tr>
<th colspan="6"><h1>Item Available</h1></th>
</tr>
<tr>
<th>product id</th>
<th>prorduct name</th>
<th>description</th>
<th>Price($)</th>
<th>last updated</th>
<th>Quantity</th>
</tr>
";
//-------connect to MySQL database system------------------------------------
$dbname="csc395_6";
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn ){ print "Could not connect: " . mysqli_error($conn);}
//-------choose a database to use------------------------------------
mysqli_select_db($conn, $dbname);
//--------retrive data from the database---------------------------------------
$sql = "SELECT * FROM products";
$retval = mysqli_query($conn, $sql);
if (!$retval){ print "Error retrieving data from a table: " . mysqli_error($conn); }
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
$tmp=$row['p_name'];
$products["$tmp"]=$row;
}
mysqli_close($conn);
//--------------------------create product lists---------------------------------------------------
foreach($products as $p=>$a)
{
print "<tr>";
foreach($a as $k=>$v)
{
print "<td>$v</td>";
}
print "<td><input type="text" name="$p" size="5"></td></tr>";
}
print"
<tr><td></td><td></td>
<td><input type="submit" value="add to cart"></td>
<td></td><td></td><td></td>
</table>
</form>
<br>
";
//------------------------shopping cart-----------------------------------------------------
foreach($products as $p=>$v)
{
if(isset($_POST["$p"]))
{
if(is_numeric($_POST["$p"]))
{
if(isset($_SESSION['cart']["$p"]))
{
//The shopping cart already has a specific kind of fruit, but now add additional
$_SESSION['cart']["$p"]=$_SESSION['cart']["$p"]+$_POST["$p"];
}
else
{ //The shopping cart has no a specific kind of fruit, but now add some
$_SESSION['cart']["$p"]=$_POST["$p"];
}
}
elseif($_POST["$p"]=="Remove")
{
//remove the selected products from the shopping cart
unset($_SESSION['cart']["$p"]);
}
}
}
print"
<fieldset>
<legend>Your Shopping Cart</legend>";
if(!isset($_SESSION['cart']))
{
$_SESSION['cart']=array();
print "Your shopping cart is empty";
}
else
{
print "
<form action ="carttest_3.php" method="post">
<table border="0">
<tr>
<th>Item</th>
<th>Quantity</th>
<th>total_each_item</th>
<th></th>
</tr>";
foreach($_SESSION['cart'] as $key=>$value)
{
print "
<tr>
<td>$key</td>
<td>$value</td>
<td>"$k=$value*$P"</td>"
<?php
$s=$k;
$k=$s+$k;
?>
<?php
print"
<td><input type="submit" name="$key" value="Remove"></td>
</tr>";
}
print "
<tr>
<td>total_cost_all_kind_of_fruits:</td>
<td>$k</td>
</tr>
<td></td>
<td></td>
<td><input type="submit" name="$key" value="Remove"></td>
</tr>";
print "</table></form>";
}
print"
</fieldset>
</body>
</html>";
?>
CREATE DATABASE csc395_6;
CREATE TABLE products(
p_id INT UNSIGNED NOT NULL PRIMARY KEY,
p_name VARCHAR(40) NOT NULL,
p_description VARCHAR(200) NOT NULL,
p_price INT NOT NULL,
add_date DATETIME NOT NULL,
);
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(1, "grape","green grape", 9, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(2, "berry","red berry", 8, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(3, "cherry","red cherry", 7, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(4, "crabapple","red crabapple", 6, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(5, "cantaloupe","sweet cantaloupe", 5, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(6, "apple","green apple", 4, NOW());
INSERT INTO products (p_id, p_name, p_description, p_price, add_date) VALUES
(7, "orange","green apple", 4, NOW());
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.