Automatically incrementing the SESSION key identifier

I have a problem with something. I have this code snippet to add an item to the cart:

$product_id = isset($_GET['product_id']) ? $_GET['product_id'] : "";
$product_name = isset($_GET['product_name']) ? $_GET['product_name'] : "";

$sql = "SELECT * FROM products WHERE product_id LIKE '{$product_id}' AND product_name LIKE '{$product_name}' LIMIT 1";
$stmt = $connection->prepare($sql);
$stmt->execute();

$num = $stmt->rowCount();

if($num == 1)
{
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        extract($row);
        if(!isset($_SESSION['cart']))
        {
            $product_id_session = 1;
        }
        else
        {
            $count = count($_SESSION['cart']); 
            $product_id_session = $count++;
        }

        $columns = array
        (
            'product_id_session' => $product_id_session,
            'product_id' => $product_id,
            'product_name' => $product_name,
            'product_price' => $product_price           
        );
        $_SESSION['cart'][$product_id_session] = $columns;      
        redirect_to('products.php?&message=added&product_name='. $_SESSION['cart'][$product_id_session]['product_name']);
    }
}

As you can see, if a session is created cart, I assign a variable to the $product_id_sessionSESSION array counter plus one. Otherwise, the variable is $product_id_sessionset to 1. On the cart page, I have a link to delete the selected product:

foreach($_SESSION['cart'] as $product)
{
    echo "<button onClick=\"location.href='remove.php?product_id_session={$product['product_id_session']}'\">
    Remove from cart
    </button>";
}

Then in the remove.php file I have this to process the data from the Query String and remove the product from the recycle bin:

$product_id_session = isset($_GET['product_id_session']) ? $_GET['product_id_session'] : "";
unset($_SESSION['cart'][$product_id_session]);

, : , . . , , , , $product_id_session . ? ?

+4
2

:

$_SESSION['cart'][] = $columns;

.

( )

$_SESSION['cart'] = array_values($_SESSION['cart']);

foreach, , $index. $index = > $.

foreach($_SESSION['cart'] as $index=>$product)
{
    echo "<button onClick=\"location.href='remove.php?product_id_session={$index}'\">
    Remove from cart
    </button>";
}

Remove.php , :

if (isset($_GET['product_id_session']) and $_GET['product_id_session']) {
    $product_id_session = $_GET['product_id_session'];
    unset($_SESSION['cart'][$product_id_session]);
}
+1

, , , :

if($num == 1) {
    $row = $stmt->fetch(PDO::FETCH_ASSOC); // no need for the loop as you only have 1 result
    extract($row);
    if(!isset($_SESSION['cart'])) {
        $_SESSION['cart'] = array();
    }
    // keep track of the added product for the time being
    if (!isset($_SESSION['cart'][$product_id])) {
        $columns = array(
            'product_id_session' => $product_id_session,
            'product_id' => $product_id,
            'product_name' => $product_name,
            'product_price' => $product_price,
            'amount' => 0, //just add last comma as good practise here
        );
        $_SESSION['cart'][$product_id] = $columns;
    }
    //raise the amount
    $_SESSION['cart'][$product_id]['amount']++;
    redirect_to('products.php?&message=added&product_name='. $_SESSION['cart'][$product_id_session]['product_name']);
}

:

foreach($_SESSION['cart'] as $product) {
    echo "<button onClick=\"location.href='remove.php?product_id={$product['product_id']}'\">Remove from cart</button>";
}

:

"" , count Id:

    if(!isset($_SESSION['cart']))
    {
        $_SESSION['cart'] = array();
        $_SERVER['cart_product_id'] = 1;
    }
    else
    {
        $_SERVER['cart_product_id']++;
        $product_id_session = $_SERVER['cart_product_id'];
    }
0

All Articles