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!
<?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.
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.
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.
<?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.