14 Replies - 6426 Views - Last Post: 03 April 2011 - 12:17 PM Rate Topic: -----

#1 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 10:08 AM

hey everyone,

i'm a begginer in PHP, and i'm trying to create a simple shopping cart with one item using sessions, i start with a dropdown menu of the sizes of that item, after choosing the size i set the quantity, then when i click sumbit it sends me to the cart page. when i get to the cart page it should show me the item i added, then the price and the total

this is what i have so far, any help is appreciated

Page1.php

I begin by setting the sessions:

<?php 
session_start(); 
if(isset($_POST['size'])) { 
  $_SESSION['S']='Small'; 
  $_SESSION['M']='Medium'; 
  $_SESSION['L']='Large'; 
  $_SESSION['XL']='XLarge'; 
}
?>





Then i enter the dropdown menu and the quantity using HTML:

<html>
	<body>
		
		<p>ORDER your Pants</p>
		<form id="pants" method="post" action="page2.php">
			<p>
				<select name="Size">
					<option value="invalid">Choose size</option>
					<option value="S">Small</option>
					<option value="M">Medium</option>
					<option value="L">Large</option>
					<option value="XL">Extra Large</option>
				</select>
				<br />
				Quantity: <input type="text" value="1" name="Quantity" /><br />
				<input type="Submit" value="Add to cart" name="Add to cart" />
			</p>
		</form>
	</body>
</html>




in the cart page which is, Page2.php

I first start my session

 <?php session_start(); ?> 



then add my cart table:

<html>
<body>
<table border="1" cellpadding="5">
		<tr>
		<th>Description</th>
		<th>Size</th>
		<th>Quantity</th>
		</tr>
                <tr>
                <td>
                <?php echo $_SESSION["size"]; ?>
                </td>
                <td>
                <?php echo $_SESSION["S"]; ?>
                </td><td>1</td></tr></table>
                <p>Total charges: $</p>
                <p><a href="p1.php">Continue Shopping</a></p>
                <hr />
</body>
		</html>





again any help is appreciated

Thanx,

Is This A Good Question/Topic? 0
  • +

Replies To: shopping cart using sessions, any help is appreciated.

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 10:30 AM

As a suggestion I say store the products in an array in the SESSION array.
$_SESSION['cart'][] = array('product_name' => 'shoe', 'size' => 'XL', 'quantity' => 3);
so you can access them as easily as
foreach($_SESSION['cart'] as $item) {
    printf("Item - %s, Size - %s, Quantity, %d\n", $item['product_name'], $item['size'], $item['quantity']);

Was This Post Helpful? 0
  • +
  • -

#3 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 10:33 AM

thank you for the reply,

but would this work with the drop down menu i provided?
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 10:44 AM

Yes it would.
$size = $_POST['Size'];


This code is not needed because the code above will get the value of the option selected.
$_SESSION['S']='Small'; 
$_SESSION['M']='Medium'; 
$_SESSION['L']='Large'; 
$_SESSION['XL']='XLarge'; 

Was This Post Helpful? 0
  • +
  • -

#5 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:01 AM

i'm sorry for taking alot of your time,

it gives me the following


Warning: Invalid argument supplied for foreach() in /homepages/23/d356121561/htdocs/mombo/page2.php on line 5

line 5:

foreach($_SESSION['cart'] as $item)
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:07 AM

That means that $_SESSION['cart'] isn't actually an array.
Was This Post Helpful? 0
  • +
  • -

#7 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:08 AM

so codeprada was wrong?
Was This Post Helpful? 0
  • +
  • -

#8 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:09 AM

Post your PHP code where you get the error. This error is caused because $_SESSION['cart'] isn't an array.
Was This Post Helpful? 0
  • +
  • -

#9 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:11 AM

this is exactly what i put for page2.php

<?php 
session_start();?> 
<?php 
	$size = $_POST['Size'];
	foreach($_SESSION['cart'] as $item) 
		{  
			printf("Item - %s, Size - %s, Quantity, %d\n", $item['product_name'], $item['size'], $item['quantity']); 
		}
	
	?>



and page1.php


<?php 
session_start(); 
$_SESSION['cart'][] = array('product_name' => 'pants', 'size' => 'XL', 'quantity' => 3);
?> 
<html>
	<body>
		
		<p>ORDER your Pants</p>
		<form id="pants" method="post" action="page2.php">
			<p>
				<select name="Size">
					<option value="invalid">Choose size</option>
					<option value="S">Small</option>
					<option value="M">Medium</option>
					<option value="L">Large</option>
					<option value="XL">Extra Large</option>
				</select>
				<br />
				Quantity: <input type="text" value="1" name="Quantity" /><br />
				<input type="Submit" value="Add to cart" name="Add to cart" />
			</p>
		</form>
	</body>
</html>



sorry i know i'm taking alot of your time
Was This Post Helpful? 0
  • +
  • -

#10 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:21 AM

Time isn't a problem right now :chinese: You problem is you can't store an array in $_SESSION without serializing it.

On page one use this
<?php 
session_start();
$cart = array();

if(isset($_SESSION['cart']))
     $cart = unserialize($_SESSION['cart']);  

$cart[] = array('product_name' => 'pants', 'size' => 'XL', 'quantity' => 3);

$_SESSION['cart'] = serialize($cart);
?> 


On other pages to use the Cart array from SESSION simply unserialize it.
session_start();
$cart = array();
$cart = unserialize($_SESSION['cart']);


Read up more on serialize and unserialize.

This post has been edited by codeprada: 03 April 2011 - 11:22 AM

Was This Post Helpful? 0
  • +
  • -

#11 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:30 AM

if i make page1.php exactly like this:

<?php   
	session_start();  
	$cart = array();  
	if(isset($_SESSION['cart']))  
	$cart = unserialize($_SESSION['cart']);    
	$cart[] = array('product_name' => 'pants', 'size' => 'XL', 'quantity' => 3);  
	$_SESSION['cart'] = serialize($cart);  
?> 

<html>
	<body>
		
		<p>ORDER your Pants</p>
		<form id="shirt" method="post" action="page2.php">
			<p>
				<select name="Size">
					<option value="invalid">Choose size</option>
					<option value="S">Small</option>
					<option value="M">Medium</option>
					<option value="L">Large</option>
					<option value="XL">Extra Large</option>
				</select>
				<br />
				Quantity: <input type="text" value="1" name="Quantity" /><br />
				<input type="Submit" value="Add to cart" name="Add to cart" />
			</p>
		</form>
	</body>
</html>


and page2.php like this,

<?php 
session_start();  
$cart = array();  
$cart = unserialize($_SESSION['cart']); 
?>



i get an empty page when i click add to cart
Was This Post Helpful? 0
  • +
  • -

#12 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:42 AM

Well page2.php does nothing to produce any output. There isn't any echo, print, printf, var_dump...you know functions like that.

Also place else between Line's 5 and 6 on page1

This post has been edited by codeprada: 03 April 2011 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#13 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:49 AM

i'm sorry i'm lost here <<<new to php



for page2.php, to print a table with the value of the item added to the cart,

it would be like this:

<?php 
session_start();  
$cart = array();  
$cart = unserialize($_SESSION['cart']); 
?>
<?php
foreach($_SESSION['cart'] as $item) {  

     printf("Item - %s, Size - %s, Quantity, %d\n", $item['product_name'], $item['size'], $item['quantity']); 
?>

Was This Post Helpful? 0
  • +
  • -

#14 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 11:55 AM

View Postmombo, on 03 April 2011 - 02:49 PM, said:

i'm sorry i'm lost here <<<new to php

for page2.php, to print a table with the value of the item added to the cart,

it would be like this:


Something like this
<?php 
session_start();  
if(isset($_SESSION['cart'])) ( //verify that something is actually in there before using it
	$cart = array();  
	$cart = unserialize($_SESSION['cart']); 

	foreach($cart as $item) {  //$cart is our array...$_SESSION['cart'] is a STRING representation of our array
		 printf("Item - %s, Size - %s, Quantity, %d\n", $item['product_name'], $item['size'], $item['quantity']); 
	}
}
?>

Was This Post Helpful? 0
  • +
  • -

#15 mombo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 30-March 11

Re: shopping cart using sessions, any help is appreciated.

Posted 03 April 2011 - 12:17 PM

i think i got the hang of this

thannnnnnnnnnnnnnnnnnnnnnk you so so much

i'm sorry i took alot of your time, i'm just new to this

thank you,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1