FUNCTION 1
<?php
session_start();
$connect = new mysqli("localhost", "root", "", "test");
$connect->set_charset("utf8");
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 = 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 Already Added")</script>';
echo '<script>window.location="/Sport/Sportsprodukter/basketballnet.php"</script>';
}
}
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="shoppingcart.php"</script>';
}
}
}
}
?>
FUNCTION 2
<?php
session_start();
$connect = new mysqli("localhost", "root", "", "test");
$connect->set_charset("utf8");
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 = 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("Produkt allerede tilføjet til kurven")</script>';
echo '<script>window.location="/root/Sport/Sportsprodukter/golf.php"</script>';
}
}
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;
}
}
?>
This is more of a request than a question. It also might be against the rules, but I am at my wits end. I through tutorials and StackOverflow managed to create the shopping cart above with the ability to add products with function 2. I am afraid however that I depended on the help online too much, and thus lack the experience and understand to know how it truly works. The lack of know-how has now come back to bite me in the ■■■, as I’m nearing the end of my university project in my 2nd semester, and is this project I am required to explain parts of my code.
So I come to ya’ll in the hopes of a merciful soul willing to help me with gaining an understanding of the inner works of this code, and how to best explain it. I’m not looking for the answer per say, but merely a few keywords to allow me to best explain it.