dongyu5482 2014-06-04 16:38
浏览 228
已采纳

数组值被循环中的新值覆盖

I spent a good 30 minutes looking on this site trying to find an answer to this problem however what ever I seem to try won't fix it.

Right at the end of the php code block. I create a session array called "checkout" where I want to store each of the item's ID inside. This is then passed over to a checkout page where the user will see a summary of what they have ordered/their details.

How ever when I come to the checkout page I can only ever seem to get the last $item_id added to the cart to show up

I think each time the loop runs, the $item_id value is being over written inside the "checkout" array instead of being added onto the end of the array. But I'm just not sure how to fix it.

Any help you can give would be greatly appreciated as I've been sat here for 3 hours now trying to get round this problem.

<?php

$cartOutput ="";
$cartTotal = "0";
$totalVat = "0";


if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your Shopping cart is empty</h2>";
} else {
$i = 0;
foreach ($_SESSION["cart_array"] as $each_item){

    $item_id = $each_item['item_id'];
    $sql = mysql_query("SELECT * FROM estock WHERE stockno='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)){
        $product_name = $row["description"];
        $price = $row["price"];
        $details = $row["details"];
    }


    $pricetotal = $price * $each_item['quantity'];
    $cartTotal = $pricetotal + $cartTotal;
    $totalVat = $cartTotal * 1.175;
    //Table 
    $cartOutput .= '<tr>';
    $cartOutput .= '<td> <a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name . '" width="40" height="52" border="1" />  </td>';
    $cartOutput .= '<td>'.$details.'</td>';
    $cartOutput .= '<td>£'.$price.'</td>';
    $cartOutput .= '<td>'.$each_item['quantity'].'</td>';
    $cartOutput .= '<td>£'.$pricetotal.'</td>';
    $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
    $cartOutput .= '</tr>'; 



    $_SESSION["checkout"] = array();
    $_SESSION["checkout"][] = $item_id;
    $i++;
    if ($i++ > 3) break;

}   

}

?>

Below you can see what I'm doing to retrieve the array on the checkout.php page

I then echo out $cartOutput inside a table.

foreach ($_SESSION["checkout"] as $itemid){
    $cartOutput = "";
    $item_id = $itemid;
    $sql = mysql_query("SELECT * FROM estock WHERE stockno='$item_id' LIMIT 1");
    while ($row = mysql_fetch_array($sql)){
        $item_id = $row["stockno"];
        $product_name = $row["description"];
        $price = $row["price"];
        $details = $row["details"];
    }

    $cartOutput .= '<tr>';
    $cartOutput .= '<td>'.$item_id.'</td>';
    $cartOutput .= '</tr>'; 
}
?>
  • 写回答

1条回答 默认 最新

  • dragon5006 2014-06-04 16:47
    关注

    The problem is in this line

    $_SESSION["checkout"] = array();
    

    Put this line out of the main foreach loop and in the loop add the values in the array. Because each time you are creating new array and thats why you old values are lost.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面