The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> shopping cart display
Louffeman
post Aug 12 2014, 03:40 AM
Post #1


Member
***

Group: Members
Posts: 69
Joined: 21-June 14
Member No.: 21,123



Hi, I want to build up a shopping cart,
my first script is OK for the display, but it cannot display the second or more items altogethor when I put the quantities.
and my second script didn't work, the server indicated: Parse error: syntax error, unexpected $end in C:\wampserver32\www\test\cart.php on line 35(last line).
can anyone help me to make dispaly my shopping cart in serveral items togethor ? thanks a lot !

first script:
$res=$_POST['price']*$_POST['quantity'];
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['quantity']));
$res=$_POST['price']*$_POST['quantity'];
{
echo $_POST['name'] . $_POST['price'] . $_POST['quantity'] . "$res";
}
?>
<br/><a href="index.php">Home</a>

second script:
<h1> shopping Cart</h1>
<?php
session_start();
if(isset($_POST['name'])){
$pid=$_POST['name'];
$wasfound=false;
$i=0;
//if the cart session variable is not set or cart array is empty
if(!isset($_SESSION["cart_array"])|| count($_SESSION["cart_array"])<1){
//run if the cart is empty or not set
$_SESSION["cart_array"]=array(1=>array("name"=>$pid, "quantity"=>1));
} else
//run if cart has at least one item in it
foreach($_SESSION["cart_array"] as $each_item){
$i++;
while(list($key, $value)=each($each_item)){
if($key=="name" && $value==$pid){
//that item is in cart already so let's adjust its quantity using array_spice()

array_splice($_SESSION["cart_array"], $i-1, 1,array(array("name"=>$pid, "quantity"=>$each_item['quantity'])));
$wasfound=true;
}//close if condition
}//close while loop
}//close loreach loop

$res=$_POST['price']*$_POST['quantity'];
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['quantity']));
$res=$_POST['price']*$_POST['quantity'];
{
echo $_POST['name'] . $_POST['price'] . $_POST['quantity'] . "$res";
}
?>
<br/><a href="index.php">Home</a>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Aug 12 2014, 07:04 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



This looks incorrect, and appears in both scripts:

QUOTE

$res=$_POST['price']*$_POST['quantity'];
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['quantity']));
$res=$_POST['price']*$_POST['quantity'];
{
echo $_POST['name'] . $_POST['price'] . $_POST['quantity'] . "$res";
}

The IF condition shouldn't have a semicolon before the statement, and the statement should be inside the curly braces. Perhaps like this:

CODE

if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['quantity']))
{
$res=$_POST['price']*$_POST['quantity'];
echo $_POST['name'] . $_POST['price'] . $_POST['quantity'] . "$res";
}

As a sidenote you may consider sanitizing the user data before using it, in order to prevent HTML injections, type mismatches etc that may break the script.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Louffeman
post Aug 12 2014, 02:11 PM
Post #3


Member
***

Group: Members
Posts: 69
Joined: 21-June 14
Member No.: 21,123



in fact, it is a shopping script which show the item, price, quantity and amount, but only for one item, if I want to buy more items, the script is not ok,the new item display, the old item will be disappear, So do you know how can it display serveral item togethor ?

?php>
$res=$_POST['price']*$_POST['quantity'];
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['quantity']));
$res=$_POST['price']*$_POST['quantity'];
{
echo $_POST['name'] . $_POST['price'] . $_POST['quantity'] . "$res";
}
?>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Aug 12 2014, 03:35 PM
Post #4


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



The second script seems to store old items in $_SESSION["cart_array"], so if you want to display these old items you must look for them in there.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 18th April 2024 - 07:21 PM