...

View Full Version : $_SESSION array to shopping cart



Teddybear
04-26-2012, 08:47 PM
I am trying to teach myself PHP, but not really having much luck. I have been trying to create a shopping cart. From the Order page I have three items and when I press 'BUY' beside one of the items, it puts the item purchased, an image and the quantity ordered into the shopping cart on a second page called 'Your Order. Only the second page is showing the first item only (I did get the image to and title to work), no matter which 'BUY' button I press, and although I seem to have '1' in the quantity box, I don't think it is actually working. Can you please put me out of my mysery and help. I must have tried about 17 different versions of this, but no luck. Where am I going wrong. The script starts with the session_start() at the very top, and I have probably got loads of redundant scripting in there. Please put me on the right track. Thank you

Version 17 :

<?php
if ($num_pans > 0) {
$pans = mysql_fetch_assoc($results);

$panname = ($row['panname']);
$stock = ($row['stock']);
$price = ($row['price']);
}
?>
<table>
<tr>
<td>
<img src="images/coppercasserole.jpg" alt="copper casserole pan" />
</td>
<td>
<p>Copper Casserole Pan<br />
Capacity 4 Litres</p>
</td>

<?php $query = "SELECT * FROM pans WHERE panname = 'coppercasserole'";

// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($results)) {
echo "<td>&pound";
echo $row['price']/100;
echo "</td>";
}
?>
<td>
<form method="post" action="cartaction.php">
<p>
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="coppercasserole" />
</p>

<?php
//If $_SESSION['cart'] hasn't yet been defined, define it as an array
if (!isset($_SESSION['cart'])) {

$_SESSION['cart'] = array(
copcass => array ('image' =>
'img src= "images/coppercasserole.jpg"', 'panname'=> 'Copper Casserole', 'qty' => 'qty', 'price' => 'price',
$image = image, $panname = panname, $qty = qty, $price = price)); {

//Add the Array to the SESSION

$_SESSION['image']='<img src="images/coppercasserole.jpg"/>';
$_SESSION['panname']='Copper Casserole';
$_SESSION['qty']=$qty;
$qty=0; $qty++;
}
}
?>
</form>
</td></tr>
<tr><td>
<img src="images/coppersaucepan.jpg" alt="copper saucepan" />
</td>
<td>
<p>Copper Saucepan 10"<br /></p>
</td>
<?php $query = "SELECT * FROM pans WHERE panname = 'coppersaucepan'";

// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}

while($row = mysql_fetch_assoc($results)) {
echo "<td>&pound";
echo $row['price']/100;
echo "</td>";
}
?>

<td>
<form method="post" action="cartaction.php">
<p>
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="coppersaucepan" />
</p>
<?php

//If $_SESSION['cart'] hasn't yet been defined, define it as an array
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array(
coppan => array ('image'=>
'img src ="images/coppersaucepan.jpg"', 'panname'=> 'Copper Saucepan', 'qty'=> 'qty', 'price' => 'price',
$image = image, $panname = panname, $qty = qty, $price = price)); {

//Add the Array to the SESSION

$_SESSION['image']='<img src="images/coppersaucepan.jpg"/>';
$_SESSION['panname']='Copper Saucepan';
$_SESSION['qty']=$qty;
$qty=0; $qty++;
}
}
?>
</form>
</td></tr>
<tr><td>
<img src="images/coppersaute.jpg" alt="copper saute pan" />
</td>
<td>
<p>Copper Saut&eacute; Pan 11"<br /></p>
</td>
<?php $query = "SELECT * FROM pans WHERE panname = 'coppersaute'";

// submit the query to the database
$results = @mysql_query($query, $connection); if (!results) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($results)) {
echo "<td>&pound";
echo $row['price']/100;
echo "</td>";
}
?>
<td>
<form method="post" action="cartaction.php">
<p>
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="coppersaute" />
</p>
<?php
//If $_SESSION['cart'] hasn't yet been defined, define it as an array
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array(
copsaute => array ('image' =>
'img src ="images/coppersaute.jpg"', 'panname' => 'Copper Saute', 'qty' => 'qty', 'price' => 'price',
$image = image, $panname = panname, $qty = qty, $price = price)); {

//Add the Array to the SESSION

$_SESSION['image']='<img src="images/coppersaute.jpg"/>';
$_SESSION['panname']='Copper Saut&eacute Pan';
$_SESSION['qty']=$qty;
$qty=0; $qty++;
}
}
?>
</form>
</td></tr>
</table>
</div>

mlseim
04-26-2012, 09:17 PM
Repost your script(s), but put PHP brackets (wrap codes) around each one so I can tell where one
script ends and the other one begins. Looking at your original post ... how do we know which script is which?

unless what you have posted is only one script? ??

... and if it has session_start(); at the top, why don't I see that?
what are you showing us, and what are you not showing us. very confusing to look at.


.

Teddybear
04-27-2012, 08:01 PM
Thanks, told you I didn't know what I was doing. Is a php wrap code <?php and ?>, if so I have got those around the php script so I have now excentuated them with '// start php and //end php', hope thats right. The message under the block where I put the message/script was very confusing for me, so I hope that I've got it right this time. Whole script is:


<?php //start php
session_start();

// Connect to the database and select the correct database.
$connection = @mysql_connect('localhost', 'username', 'password')
or die('Connection error: ' . mysql_error());
mysql_select_db('database', $connection)
or die('DB selection error: ' . mysql_error());


// Check that we have all required variables and they're not just
// empty. If we don't, then go back to the form page
if (( !isset($_POST['panname'])) || ( !isset($_POST['stock'])) ||
( !isset($_POST['price'])) ||
( empty($_POST['panname'])) || ( empty($_POST['stock'])) ||
( empty($_POST['price'])) ) {

}

?> // end php

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Copper Range of Pans</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
</head>
<body>


<div id="header">
<h1>Copper Pans Range</h1>
</div>


<div id="mainblock">

<?php // start php
if ($num_pans > 0) {
$pans = mysql_fetch_assoc($results);

$panname = ($row['panname']);
$stock = ($row['stock']);
$price = ($row['price']);
}
?> // end php
<table>
<tr><td>
<img src="images/coppercasserole.jpg" alt="copper casserole pan" />
</td>
<td>
<p>Copper Casserole Pan<br />
Capacity 4 Litres</p>
</td>

// Start php
<?php $query = "SELECT * FROM pans WHERE panname = 'coppercasserole'";

// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($results)) {
echo "<td>&pound";
echo $row['price']/100;
echo "</td>";
}
?> // end php

<td>
<form method="post" action="cartaction.php">
<p><input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="coppercasserole" />
</p>

<?php // start php
//If $_SESSION['cart'] hasn't yet been defined, define it as an array
if (!isset($_SESSION['cart'])) {

$_SESSION['cart'] = array(
copcass => array ('image' =>
'img src= "images/coppercasserole.jpg"', 'panname'=> 'Copper
Casserole', 'qty' => 'qty', 'price' => 'price',
$image = image, $panname = panname, $qty = qty, $price = price)); {

//Add the Array to the SESSION

$_SESSION['image']='<img src="images/coppercasserole.jpg"/>';
$_SESSION['panname']='Copper Casserole';
$_SESSION['qty']=$qty;
$qty=0; $qty++;
}
}
?> // end php

</form>
</td></tr>
<tr><td>
<img src="images/coppersaucepan.jpg" alt="copper saucepan" />
</td>
<td>
<p>Copper Saucepan 10"<br /></p>
</td>

<?php // start php
$query = "SELECT * FROM pans WHERE panname = 'coppersaucepan'";

// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}

while($row = mysql_fetch_assoc($results)) {
echo "<td>&pound";
echo $row['price']/100;
echo "</td>";
}
?> // end php

<td>
<form method="post" action="cartaction.php">
<p>
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="coppersaucepan" />
</p>

<?php // start php
//If $_SESSION['cart'] hasn't yet been defined, define it as an array
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array(
coppan => array ('image'=>
'img src ="images/coppersaucepan.jpg"', 'panname'=> 'Copper
Saucepan', 'qty'=> 'qty', 'price' => 'price',
$image = image, $panname = panname, $qty = qty, $price = price)); {

//Add the Array to the SESSION

$_SESSION['image']='<img src="images/coppersaucepan.jpg"/>';
$_SESSION['panname']='Copper Saucepan';
$_SESSION['qty']=$qty;
$qty=0; $qty++;
}
}
?> // end php

</form>
</td></tr>
<tr><td>
<img src="images/coppersaute.jpg" alt="copper saute pan" />
</td>
<td>
<p>Copper Saut&eacute; Pan 11"<br /></p>
</td>

<?php // start php
$query = "SELECT * FROM pans WHERE panname = 'coppersaute'";

// submit the query to the database
$results = @mysql_query($query, $connection); if (!results) {
die(mysql_error());
}
while($row = mysql_fetch_assoc($results)) {
echo "<td>&pound";
echo $row['price']/100;
echo "</td>";
}
?> // end php

<td>
<form method="post" action="cartaction.php">
<p>
<input type="submit" name="submit" value="Buy" />
<input type="hidden" name="cartaction" value="add" />
<input type="hidden" name="item" value="coppersaute" />
</p>

<?php // start php
//If $_SESSION['cart'] hasn't yet been defined, define it as an array
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = array(
copsaute => array ('image' =>
'img src ="images/coppersaute.jpg"', 'panname' => 'Copper Saute',
'qty' => 'qty', 'price' => 'price',
$image = image, $panname = panname, $qty = qty, $price = price)); {

//Add the Array to the SESSION

$_SESSION['image']='<img src="images/coppersaute.jpg"/>';
$_SESSION['panname']='Copper Saut&eacute Pan';
$_SESSION['qty']=$qty;
$qty=0; $qty++;
}
}
?> // end php

</form>
</td></tr>
</table>
</div>

<div id="footer">
<form method="post" action="cartaction.php">
<p>
<input type="submit" name="submit" value="Show Cart or Checkout" />
<input type="hidden" name="cartaction" value="display" />
</p>
</form>

<ul class="navlist">
<li><a href="index.php">Go Back Home</a></li>
</ul>
</div>

</body>
</html>

mlseim
04-27-2012, 09:00 PM
Here's a thing I don't quite understand ...

Lines like this ....

$_SESSION['image']='<img src="images/coppersaute.jpg"/>';
$_SESSION['panname']='Copper Saut&eacute Pan';
$_SESSION['qty']=$qty;

Why are you putting images and names in for SESSION variables?
Isn't the user supposed to pick those from a list?
What if I'm the user and I don't select a Copper Saute pan?


.

Teddybear
04-28-2012, 10:07 AM
This is the list, there are three items in my database for this product (copper pans), the Copper Casserole pan, the Copper Saucepan and the Copper Saute pan. When the user clicks on the 'Buy' button at the side of the table enclosing a picture, the title/dimensions and the price, I need the image, the pan name and the quantity ordered to be visible in a separate page called 'cartaction.php. - 'YOUR ORDER'. Having fiddled with the coding over and over again, I can only get one image/name/qty to be visible, the first one only - Copper Casserole, it appears no matter which 'Buy' button I press.

This is the scripting for the 'cartaction.php' page:


<?php // sart code
session_start();

// Connect to the database and select the correct database.
$connection = @mysql_connect('localhost', 'username', 'password')
or die('Connection error: ' . mysql_error());
mysql_select_db('database', $connection)
or die('DB selection error: ' . mysql_error());

?> // end code

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Pans for Your Kitchen</title>
<link rel="stylesheet" href="styles/site.css" type="text/css" />
</head>
<body>

<div id="header">
<h1>Your Shopping Cart</h1>
<?php

//iterate through the cart to show the product and the quantity purchased

// Create code to display items purchased

$image = $_SESSION['image'];
$panname = $_SESSION['panname'];
$qty= $_SESSION['qty'];
$price=$_SESSION['price'];
$qty = preg_replace('#[^0-9]#i', '', $qty); // filter everything but numbers
$qty=0; $qty++;

$_SESSION = array('$image', '$panname', '$qty', '$price');
foreach ($_SESSION as $array) {
//while ($_SESSION == $cart)


if(isset($_SESSION['cart'])) { // so if the cart isn't empty - show the purchases

echo "<table padding=\"3\">"; // start the cart display formatting

//display in table format
echo "<tr>";
echo "<td>$image</td>";
echo "<td>$panname</td>";
echo "<td><input type='text' value='$qty' size='2' maxlength='2' name='qty' /></td>";
echo "<td ><input type='button' name='update' value='Update This One Item' size='20'
maxlength='20' /></td>";
echo "</tr>";
echo "</table>";
}
}

foreach ($_SESSION as $cart => $price) {

$query = "INSERT INTO ._tables ('$panname', '$qty', '$price')";

// submit the query to the database
$results = @mysql_query($query, $connection);
if (!results) {
die(mysql_error());
}
}
?> // end code

</div>


<div id="footer">
<form method="post" action="checkout.php">
<p><input type="submit" name="submit" value="Goto Checkout" /></p>
</form>

<ul class="navlist">
<li><a href="copperrange.php">Copper Range</a></li>
<li><a href="steelrange.php">Steel Range</a></li>
<li><a href="index.php">Go Back Home</a></li>
</ul>
</div>

</body>
</html>

mlseim
04-28-2012, 08:49 PM
Sorry for dragging this out ... it's hard because I can't test anything, nor can
I see it working in real life. You are using a database, so I would guess each
item you're selling has a row in a table? For the user who is shoppng, why not just
keep track of the item ID and quantity? That's the only SESSION variables you need.
Whenever you display anything, you render the shopping cart by the ID and quantity in the SESSION.
Query the table by ID and you have the item photo(s), description, price, availability, etc.
everything you need to display the item(s) based on the ID(s) the user has picked.

I'm stuck on the issue of the image and pan name in the SESSION. I can't get past
not using item IDs for the items the user has selected. That has me stumped.

But you have scripting done, so I'm trying to find where the problem is occurring.
Maybe I'll see it ... or someone else might see it first.


What is this supposed to do:
$query = "INSERT INTO ._tables ('$panname', '$qty', '$price')";

If the user has their selected items saved in a session, why would any table inserts be needed?
Again ... I can't figure this out. sorry.



.

Teddybear
04-29-2012, 12:55 PM
This is my problem, the scripting itself is not giving me any errors so I can't see why its not working because it's not showing me were the error is coming from. As for the item id, do you mean calling each one with a number eg. item 0, item 1, item 2 etc? Would than not be laborious if you had 2000 products to sell? Hey, but if it works for now..... Do you think you could offer an example script for that?

The INSERT bit on the other page was for me to put the all items purchased into a temporary database so that I could then submit the total of the purchased and total price in yet another page, eg: @The sum of your purchases is 63.27. Hope that explains it.

Thanks for sticking with me, I know how difficult it can be without the associated programmes let alone reading someone else scripting.

mlseim
04-29-2012, 06:22 PM
When you create a MySQL table, you have a column (usually the first column)
that automatically increments each time a row is added. That incremented (or ID) value
never repeats and is always unique. You don't have to worry about giving it a value,
it happens automatically each time you add an item (INSERT) to your inventory.

This is the best way I can explain it:
http://www.qualitycodes.com/tutorial.php?articleid=25&title=Tutorial-Building-a-shopping-cart-in-PHP

As it says, you only need one table for the actual cart. See their demo also.

The SESSION is two dimensional ... the item ID and Qty.
As long as you know those two things, you can loop thru the SESSION and
create the output by querying the database for the photos, desc, cost, etc.

Teddybear
04-29-2012, 08:43 PM
Thank you, yes of course, auto increment in the database. Thanks for sticking with me

cyberhostbd
04-30-2012, 09:55 AM
Thank you Mlseim... The resources you provided are excellent learning resources.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum