-1
I don’t have much idea how to use array, let alone involve php in it, so I took this code from the internet and managed to make some changes that I found necessary to fit well into the site I’m developing for a school project. I changed a lot of things with the exception of this part of the code, in which she adds items to a cart (Does it perfectly) but when I try to add the same item a second time, instead of it adding up the quantities it just issues an Alert saying that the item has already been added.
I’ve seen many tutorials on how to change the values of an array, but this one specifically is within a Session, variable $ and other things that I’ve learned a little bit.
Can help?
if (isset($_POST["add_to_cart"])) {
if (isset($_SESSION["shopping_cart"])) {
$item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
if (!in_array($_GET["id"], $item_array_id)) {
$count = count($_SESSION["shopping_cart"]);
$item_array = [
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"],
];
$_SESSION["shopping_cart"][$count] = $item_array;
} else {
echo '<script>alert("Item ja adicionado")</script>';
echo '<script>window.location="foodlist.php"</script>';
}
Edit1: I don’t know if it’s necessary, but on the same page you have more this array code.
else
{
$item_array = array(
'item_id' => $_GET["id"],
'item_name' => $_POST["hidden_name"],
'item_price' => $_POST["hidden_price"],
'item_quantity' => $_POST["quantity"]
);
$_SESSION["shopping_cart"][0] = $item_array;
}
}
if (isset($_GET["action"]))
{
if ($_GET["action"] == "delete")
{
foreach ($_SESSION["shopping_cart"] as $keys => $values)
{
if ($values["item_id"] == $_GET["id"])
{
unset($_SESSION["shopping_cart"][$keys]);
echo '<script>window.location="foodlist.php"</script>';
}
}
}
}
Personally I think it is irresponsible to be willing to do an e-commerce program (or any other application aimed at the market) without knowing how to program. I do not want to participate in this code. As the question is not clear and presents broad conceptual errors I vote to close.
– Augusto Vasques
I published a reply for educational purposes, but I agree with Augusto’s vision;
– Leonardo Getulio
Sorry, I did not specify but the site is not for the purpose of selling a product but for a school project even, I am still taking a technical course.
– Cristian Dos Santos de Oliveir
If that is the case, ask the question that it is a school project, because in the third line you state that you are developing a website
...no site que estou desenvolvendo..
.– Augusto Vasques