Shopping Cart $_SESSION['item'];

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Shopping Cart $_SESSION['item'];

Post by ianhull »

Hi Guys,

How would I go about adding all the clicked on products into a session array.

This is how I am sending the data to the page.

Code: Select all

<a href="add-to-cart.php?itemid=42">Add To Cart</a>

Code: Select all

<?php session_start();

$itemid = $_GET['itemid'];

//My Query To Select Price And Availablility
itemid, price, stock

//Here I need To Store Thie Product Into A Session But Don't Know How.


Please help.

Thanks
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all


<?php session_start(); 

$itemid = $_GET['itemid']; 

//My Query To Select Price And Availablility 
itemid, itemprice, stock 


///IS THIS VALID?
$_SESSION['myCart'][0] = $itemid;
$_SESSION['myCartPrice'][0] = $itemPrice;

visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

Post by visitor-Q »

well, to be sure, you'd need to post more code, and a more detailed description of what is going on with your script. but maybe this will get you goin.

Code: Select all

<?php
        if($isset($_POST['item'])){
                $_SESSION['itemArray'] = $_POST['item'];
        }
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Code: Select all

session_start(); 
///IS THIS VALID?
$_SESSION['myCart'][0] = $itemid;
$_SESSION['myCartPrice'][0] = $itemPrice;
I certainly looks valid. $_SESSION can be used like any array and being a superglobal is always available. I guess I would ask, "Have you tried the code you posted?" You really need to experiment with putting data into the session and getting it back out to get a feel for it.
(#10850)
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Thanks Guys,

Heres what I have just put together,

I think it will work but any opinions advice very welcome.

Code: Select all


<?php session_start();

$id = $_REQUEST['id'];

include_once("connection.php");

$checkProduct = mysql_query("SELECT
id,
price,
stock FROM products WHERE id = '$id'")or die(mysql_error());

while ($checkProductRecieved = mysql_fetch_array($checkProduct)){
    extract($checkProductRecieved);
    };
	
if ($stock == ""){
    $addProduct = "No";
    };
	
if ($_SESSION['myCart'][0] == ""){
    $i = 0;
    } else {
	$i = $_SESSION['i']++;
	};
	
if ($addProduct == "No"){
    //do nothing
    } else {
    $_SESSION['myCart'][$i] = $id;
    $_SESSION['myCartPrice'][$i] = $price;
	$_SESSION['i'] = $i;
	};
?>

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Here are some ideas:

Code: Select all


<?php session_start();

$id = (int)$_REQUEST['id'];     // convert to int so you don't get SQL injection

include_once("connection.php");

$result = mysql_query("SELECT
id,
price,
stock FROM products WHERE id = '$id'")or die(mysql_error());

$row= mysql_fetch_assoc($result);
	
// Add product if stock
if ($row['stock'] != ""){

    if (! isset($_SESSION['myCart'][0])){
        $i = 0;
    } else {
        $i = count($_SESSION['myCart']);   // size of array is next index
    }
	
     $_SESSION['myCart'][$i] = $row;
}

// Show cart
if (isset($_SESSION['myCart'])) {
    while ($_SESSION['myCart'] as $item) {
        echo "id={$row['id']}, price={$row['price']}, stock={$row['stock']}<br/>";
    }
} else {
    echo "No items in cart.<br/>";
}
?>

Last edited by Christopher on Mon Feb 05, 2007 2:55 am, edited 1 time in total.
(#10850)
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Wow thanks arborint,

Very much appreciated :)

I will have a go with this.

Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Play around with it to get a feel for how things might work. At some point you will want to put code into functions and ultimately into a class.
(#10850)
Post Reply