I am trying to write a page where I have the following items:

A product category, within the product category unique items to that category, and a qty where a user can select "X" amount of said unique items within each category.

The format would look like this when selected in the cart:

`Item Category
Each unique id of said Category
Qty of Category Ordered`

This is how I have each piece defined.

`$product_name = $_POST["product_name"]` - The Category

`$product_id = $_POST["product_id"];` - The unique identifier of each item within the category.

`$product_qty = $_POST["product_qty"]` - The Quantity Selected

$product\_id is a string. Let's say I have unique IDs of 2,4,6,8, so that when a quantity of 1 is selected, only ID 2 is chosen. If quantity 4 is selected, then IDs 2,4,6,8 are selected. Because of this, I make the string into an array using explode so that each ID within the string is unique.

`$product_ids = explode(',', $product_id);`

So all my values are represented, and I can successfully get them from the main page. But I can't figure out how to make the cart itself function properly. The category itself should remain static after it is added to the cart unless it is deleted from the cart - only the unique ids within the category should change. If a user selects quantity 2, but then decides to select quantity 4 instead, I need the cart to add or subtract the appropriate IDs under the one category without adding a new category. The only time a new category should be added is if the user selects the quantity from another category. Does anyone know how I can accomplish what I'm trying to do?

EDIT: Let me paste the cart code, but be gentle with me as this is my first attempt at this. This is what I am attempting to do, but it doesn't work the way I am wanting it to. Note that I have a lot of variables defined because I was testing many options. I don't necessarily need them all.

```
`<?php  
 session_start();
 include_once("config.php");

 //empty cart by distroying current session
 if(isset($_GET["emptycart"]) && $_GET["emptycart"]==1) {
     $return_url = base64_decode($_GET["return_url"]); //return url
     session_destroy();
      header('Location:'.$return_url);
  }

 //add item in cart
 if(isset($_POST["type"]) && $_POST["type"]=='add') {
     $product_name = $_POST["product_name"];
     $product_qty = $_POST["product_qty"];
     $return_url = base64_decode($_POST["return_url"]); //return url
     $product_id = $_POST["product_id"];
     $product_ids = explode(',', $product_id);
     $product_select = implode(',', array_slice($product_ids, 0, $product_qty));
     $product_final = explode(',', $product_select);

     foreach ($product_final as $id) {
         $results = $mysqli->query("SELECT * FROM products WHERE id = '$id'");
     }
     $obj = $results->fetch_object();
     if ($results) {
         $new_product = array(array('name'=>$product_name, 'code'=>$id, 'qty'=>$product_qty));
         if(isset($_SESSION["products"])) {
             $found = false;
             foreach ($_SESSION["products"] as $cart_itm) {
                 if ($cart_itm["code"] == $id) {
                     $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty);
                     $found = true;
                 }
                 else {
                     $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"]);
                 }
             }
             if($found == false) {
                 $_SESSION["products"] = array_merge($product, $new_product);
             }
             else {
                 $_SESSION["products"] = $product;
             }
         }
         else {
             $_SESSION["products"] = $new_product;
         }
     }
     header('Location:'.$return_url);
 }
 if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"])) {
     $id = $_GET["removep"];
     $return_url = base64_decode($_GET["return_url"]);
     foreach ($_SESSION["products"] as $cart_itm) {
         if($cart_itm["code"]!=$id) {
            $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"]);
         }
         $_SESSION["products"] = $product;
     }
     header('Location:'.$return_url);
 }
 ?>`

```

Another Edit:

A version where I take the $product\_select string out of the newly defined array. The cart functions the way I would want now, but then...how do I pass the $product\_select string on to the next page?

```
`<?php  
 session_start();
 include_once("config.php");

 //empty cart by distroying current session
 if(isset($_GET["emptycart"]) && $_GET["emptycart"]==1) {
     $return_url = base64_decode($_GET["return_url"]); //return url
     session_destroy();
      header('Location:'.$return_url);
  }

 //add item in cart
 if(isset($_POST["type"]) && $_POST["type"]=='add') {
     $product_name = $_POST["product_name"];
     $product_qty = $_POST["product_qty"];
     $return_url = base64_decode($_POST["return_url"]); //return url
     $product_id = $_POST["product_id"];
     $product_ids = explode(',', $product_id);
     $product_select = implode(',', array_slice($product_ids, 0, $product_qty));

     $results = $mysqli->query("SELECT * FROM products WHERE id = '$product_select'");

     if ($results) {
         $new_product = array(array('name'=>$product_name, 'qty'=>$product_qty));
         if(isset($_SESSION["products"])) {
             $found = false;
             foreach ($_SESSION["products"] as $cart_itm) {
                 if ($cart_itm["name"] == $product_name) {
                     $product[] = array('name'=>$cart_itm["name"], 'qty'=>$product_qty);
                     $found = true;
                 }
                 else {
                     $product[] = array('name'=>$cart_itm["name"], 'qty'=>$cart_itm["qty"]);
                 }
             }
             if($found == false) {
                 $_SESSION["products"] = array_merge($product, $new_product);
             }
             else {
                 $_SESSION["products"] = $product;
             }
         }
         else {
             $_SESSION["products"] = $new_product;
         }
     }
     header('Location:'.$return_url);
 }
 if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"])) {
     $product_name = $_GET["removep"];
     $return_url = base64_decode($_GET["return_url"]);
     foreach ($_SESSION["products"] as $cart_itm) {
         if($cart_itm["name"]!=$product_name) {
            $product[] = array('name'=>$cart_itm["name"], 'qty'=>$cart_itm["qty"]);
         }
         $_SESSION["products"] = $product;
     }
     header('Location:'.$return_url);
 }
 ?>`

```