Page 1 of 1
Shopping Cart $_SESSION['item'];
Posted: Mon Feb 05, 2007 1:54 am
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
Posted: Mon Feb 05, 2007 2:02 am
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;
Posted: Mon Feb 05, 2007 2:03 am
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'];
}
?>
Posted: Mon Feb 05, 2007 2:12 am
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.
Posted: Mon Feb 05, 2007 2:18 am
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;
};
?>
Posted: Mon Feb 05, 2007 2:33 am
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/>";
}
?>
Posted: Mon Feb 05, 2007 2:40 am
by ianhull
Wow thanks arborint,
Very much appreciated
I will have a go with this.
Thanks
Posted: Mon Feb 05, 2007 2:58 am
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.