dongxiezhi0590 2019-01-18 18:34
浏览 89

将数组键传递给变量

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!

  • 写回答

1条回答 默认 最新

  • douxuanyi2813 2019-01-18 20:16
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度