0

I have a script which puts products into a shopping cart;

if (isset($_POST["top"])) {
    $name = $_POST["name"];

    $_SESSION[$$name] += 1;

    $$name = $_SESSION[$$name];
    $name = $name.$$name;

    $piid = $_SESSION["piid"];
    $prod = $_POST["prod"];

    $_SESSION["cart"][$name] = array("id" => $prod, "name" => $_POST["name"], "quantity" => 1, "des" => $_POST["des"]);
    foreach ($piid as $value) {
        $ab = $value[id];
        $qty = $_POST["htop".$ab];

        if ($qty > 0) {
            $piid[] = array("id" => $row["ID"], "des" => $row["des"], "hid" => $row["hide"]);

            $_SESSION["cart"][$name]["top".$value[id]] = array("id" => $value[id], "dec" => $value[des], "qty" => $qty);
        }
    }
} else {
    $name = $_POST["name"];

    $name = $name.$$name;
    if (isset($_SESSION['cart'][$name]) && ($_SESSION['cart'][$name]['des'] === $_POST['des'])) {
        $_SESSION['cart'][$name]['quantity'] += 1;
    } elseif (isset($_SESSION['cart'][$name]) && ($_SESSION['cart'][$name]['des'] <> $_POST['des'])) {
        $_SESSION[$$name] += 1;

        $name = $_SESSION[$$name];
        $_SESSION["cart"][$name] = array("id" => $_POST["prod"], "name" => $_POST["name"], "quantity" => 1, "des" => $_POST["des"]);
    } else {
        $_SESSION["cart"][$name] = array("id" => $_POST["prod"], "name" => $_POST["name"], "quantity" => 1, "des" => $_POST["des"]);
    }
}

To avoid confusion in the array when items have different description it will set a multi-dimensional array by using the product's name and an incremental id (where required)

Now my question is how do I get a remove button to work something like this? I need to pass that sub-array's name/key as a variable so we can then pass that back to the POST method.

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" 
   enctype="application/x-www-form-urlencoded">
<button type="submit">Remove
<input type="hidden" name="rprid" value="' .$name. '" />
<button</form></div>

Thanks!

Dharman
  • 21,838
  • 18
  • 57
  • 107
Simon
  • 325
  • 1
  • 13
  • 1
    Could you make this code a little bit clearer? The comments do not help at all. – Dharman Jan 18 '19 at 18:38
  • @ficuscr and more... `$name=$name.$$name;` – Dharman Jan 18 '19 at 18:40
  • @Dharman had to paste into a script and test, actually is valid if very confusing I think. – ficuscr Jan 18 '19 at 18:41
  • I didn't say it wasn't. My point is that it is extremely unreadable. – Dharman Jan 18 '19 at 18:42
  • Oh for sure. Not disagreeing. And first few lines I think spit out 5 notices... Should refactor/clean it up a bit ideally. – ficuscr Jan 18 '19 at 18:43
  • 1
    I got lost in that code like 10 times already. Which `$name` ends up at the end? – Dharman Jan 18 '19 at 18:46
  • 3
    It rarely is a good idea to use variable variables. – trincot Jan 18 '19 at 18:48
  • @trincot couldn't think of another dynamic way to do it. I'm in an urgent crisis to fix this, I need to get live ASAP as I have orders coming out my ears, any fix will do please :) Thanks in advance all! (p.s. all, I tidied up the code a bit, sorry about that!) – Simon Jan 18 '19 at 18:49
  • 3
    @Simon Honestly, if you need something "urgently", this isn't really the best place to be. Hire someone if you can't do it yourself. – Patrick Q Jan 18 '19 at 18:50
  • Could you please use proper code formatting when posting your question on StackOverflow? – Dharman Jan 18 '19 at 18:55
  • @Dharman sure, if you could fix the formatting tool on this site so I can copy and paste my code then sure, it takes time to reformat my code before I can submit it so please – Simon Jan 18 '19 at 18:57
  • You should use code formatter build into your IDE e.g. VS code. And on stack overflow you select block of code and press CTRL + K – Dharman Jan 18 '19 at 19:00
  • Thanks bud, wish that instruction come on the page! :) – Simon Jan 18 '19 at 19:09
  • Please, for the love of all things green, turn on error reporting and fix the myriad of warnings that you'll see - https://stackoverflow.com/a/21429652/296555. Once you've done that hit Ctrl-A + DEL... seriously. This ball of spaghetti is wound so tightly an alley cat wouldn't touch it. There are some serious bugs + WTFs in here too `$$name`, `$value[id]`, `$value[des]`. Only under very specific conditions is the code doing what you hope it is doing. The rest of the time, you'll be chasing down bugs. – waterloomatt Jan 21 '19 at 13:11

1 Answers1

0

I've managed to figure out the code I need which should be a follows;

foreach($cart as $key => $value)
    {
        echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post" enctype="application/x-www-form-urlencoded"><button type="submit">Remove<input type="hidden" name="rprid" value="' .$key. '" /></form></div>';
}

Thanks for the thoughts all!

Edit: The solution is to use $key => $value that way you can simply use $key to return the key which you are looking at.

Simon
  • 325
  • 1
  • 13