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!
i know shopping cart shd sounds early for experts but to a noob like me!! it do sounds easy at first but when i started 'coding' i'm struck!! anybody there can lend a helping hand?
The below coding is a part thats called when you click on the update button of the shopping cart.. i'm using cookie for this time.. any other better way in doing please do let me know! and oh ya.. there's error on the part where the condition checks on the 0 that the user entered in for the qty. If the user entered in 0 or checked on the remove checkboxes, it will then take the cookie out and move the cookie below up(its in array).
i simply can't get it right!! its up at my neck now!! anyone help?!?! :(:(
I'm not good enough to help you with most of this, even though I'll survey it and try. However, I can warn you of one thing.. Cookies might not be the way to go, hopefully someone can prove me wrong, or right, so you know. I say this because, if cookies are disabled then you're waste deep in trouble.
I will attempt to help but like i said, i'm not the go to guy at this point.
ya, I recommend using Session variables, which you start using session_start() to accomplish, also, maybe you could modify your original post and state more clearly what exactly you are having trouble with?
// Adds product to the cart.
if (is_numeric($_POST['itemid'])) { // Check for a product id.
$itemid = $_POST['itemid'];
// Check if the cart already contains one of the products.
if (isset ($_SESSION['cart'][$itemid])) {
$qty = $_SESSION['cart'][$itemid] + 1;
} else {
$qty = $_POST['qty'];
}
// Add product to the cart session variable.
$_SESSION['cart'][$itemid] = $qty;
// Show Cart
require 'view_cart.php';
}
Your code is using session_register() and $_SESSION, why? Just scrap the session_register() calls alltogether. Why? The manual explains
http://php.net/session_register wrote:
Caution
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.
Caution
This registers a global variable. If you want to register a session variable from within a function, you need to make sure to make it global using the global keyword or the $GLOBALS[] array, or use the special session arrays as noted below.
Caution
If you are using $_SESSION (or $HTTP_SESSION_VARS), do not use session_register(), session_is_registered(), and session_unregister().
hi there, there's error when the user entered 0 into the qty field. It can be detected if the user only enter once. But it enters 2 0 in a different field, it will not delete the cookies but instead, delete one and leave the other qty as 0 . After which , the user clicked once more on update button, it will delete ALL the cookies!
session? i don't know how to use. :( anyone enlighten me please!!
Never use cookies when you can use sessions. Looking at the amount of data you are sending you might want to use the database too.
Also I have a strong feeling that you have massively overcomplicated your code.
I had edited the shopping cart that bob gave me on the session shopping cart. no items was found? is there anything i need to do before i get the session? i'm just using back the codes for view cart.
if(isset($_POST['btnAdd'])){
// Adds product to the cart.
if(is_numeric($txtQty)==true && $txtQty!=0){// Check for a product id.
$intMoreQty=0;
// Check if the cart already contains one of the products.
if (isset($_SESSION['cart'][$pdtId])){
$qty =$_SESSION['cart'][$pdtId] + $txtQty;
$intMoreQty=1;
}else{
$qty = $_POST['txtQty'];
}
// Add product to the cart session variable.
$_SESSION['cart'][$itemid] = $qty;
if($intMoreQty==1){
$comments = "We had added additional quantity to your shopping cart for this item.";
}else{
$comments = "Your item had added into the cart.";
}
print("<div align=\"center\" class=\"bodyText\">$comments<p /><input type=\"button\" name=\"btnClose\" class=\"btnCloseConfirm\" value=\"Close This Window\"onClick=\"window.close();\"></div>");
}elseif($txtQty<=0){
$lblWarning="<font color=\"#990000\"><b>Please enter number more than 0.</b></font>";
}else{
$lblWarning="<font color=\"#990000\"><b>Please enter numeric values.</b></font>";
}
}
ole wrote:Never use cookies when you can use sessions. Looking at the amount of data you are sending you might want to use the database too.
Also I have a strong feeling that you have massively overcomplicated your code.
started 'coding' i'm struck!!
struck means hit: bad
stuck: good
hey guru!! i also think that i'm having over complicated my codes. i'm never good in loopings. and this is my 1st time using php!! i'm so going to be struck. Why is being stuck good?
i have the tot of placing it into the database too. But, what if the person get out of my site without checking out? does it mean that i need to manually clear it? anyone? help help..
i had managed to do away with cookies and changed it to session! thanks everyone for the help! and i realised one thing. i'm writing too much redundant codes up there!