Forum Moderators: coopster
I've been to this thread on these forums:
[webmasterworld.com...]
But I'm doing it differently to that and thats where I get stuck.
This is my hopeless atttempt at it and it does not seem to show any error yet does not show the result I'm after because it does not seem to store the post variable.
<?
session_start();
//check if session variable cart is set
if (!isset($_session['cart']){
$cartItem = array();
$_SESSION["cart"]= $cartItem;//make $_SESSION['cart'] an array
}
//if someone chose a domain and clicked add to cart which stores that domain in domainadd
if (isset($_POST['domainadd']))
{
$domain = $_POST['domainadd'];
array_push($_SESSION['cart'],$domain);//add it to the end of $_SESSION['cart'] array
}
Does my code look valid? Please help
well, I would do it a little differently but I like easy code, cause I am not an overly complex fellow. ;)
So you want to use 'cart' in the main level of the session and then add the domains to 'cart' as an array.
so if I wanted to get at the first one then I could call
echo $_SESSION['cart'][0];
and that should echo the first domain in the cart
so, if I take your code, you seem to have all the elements in there, they just aren't quite behaving
<?
session_start();
if (!isset($_session['cart']) $_SESSION["cart"]= $cartItem;
if (isset($_POST['domainadd']) &&!empty($_POST['domainadd'])) {
$domain = $_POST['domainadd'];
$_SESSION['cart'][] = $domain;
}
I took out the comments since they are in your first post and really, we both know what we're talking about, well, you do anyway.
I added an empty check on your $_POST['domainadd'] because I am just like that, it could be set but still have no value or NULL in there.
now, I changed your array push to this
$_SESSION['cart'][] = $domain;
in an array that would add the value to the next spot, I believe it works the same with SESSIONS but have never done it that way so it needs checking
the way I play with my sessions and see what is going on is to add this
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
you can add it at the top or the bottom of your code, or both for that matter, to see what is in the session and if your changes were made properly. In your case I would add it under that code, like so
<?
session_start();
if (!isset($_session['cart']) $_SESSION["cart"]= $cartItem;
if (isset($_POST['domainadd']) &&!empty($_POST['domainadd'])) {
$domain = $_POST['domainadd'];
$_SESSION['cart'][] = $domain;
}
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
see if all that helps