douyi2664 2018-11-30 06:42
浏览 39
已采纳

PHP AJAX混合匹配的结帐项目的顺序

I'm currently working on an eCommerce app where I am working with a SESSION cart. In this cart, I have two items possible to sell. If I add the first item, it adds signifies the item correctly for my records, however if the second item is the only item selected, it's value takes the place of the the assignment I have for the first value.

To give a better understanding of what I mean, here is some code:

{
    foreach($_SESSION["shopping_cart"] as $keys => $values)
    {
        $output .= '

            <div class="customer_2_item_line">

                <div class="customer_2_item_line_cell"><img src="image/'.$values['product_code'].'.jpg"><span class="customer_2_line_cell_amt">'.$values['product_quantity'].'</span></div>

                <div class="customer_2_item_line_cell"><p>ET Spork</p><p><span></span><span class="customer_2_line_cell_name">'.$values['product_name'].'</span<</p></div>

                <div class="customer_2_item_line_cell"><p><span>$</span><span class="customer_2_line_cell_total">'.$values['product_total'].'</span></p></div>

            </div>

            <hr>

        ';
        $total_price = number_format($total_price + ($values['product_quantity'] * $values['product_price']), 2);
        $total_item = $total_item + $values['product_quantity'];
        $total_price_shipping = number_format($total_price + $shipping_price, 2);
        $qty[] = $values['product_quantity'];
    }

    $output .= '

            <input type="text" id="subtotal_cost" name="subtotal_cost" value='.$total_price.'>
            <input type="text" id="shipping_total_cost" name="shipping_total_cost" value='.$shipping_price.'>
            <input type="text" id="total_cost" name="total_cost" value='.$total_price_shipping.'>
            <input type="hidden" id="item_qty1" name="item_qty1" value='.$qty[0].'>
            <input type="hidden" id="item_qty2" name="item_qty2" value='.$qty[1].'>
            <div id="customer_2_subtotal_shipping">

            <div class="customer_2_ss_line"><p>Subtotal</p><p><span>$</span><span></span></p></div>
            <div class="customer_2_ss_line"><p>Shipping</p><p><span>$</span><span></span></p></div>
            </div>
            <hr>

            <div id="customer_2_total">
            <p>Total</p>
                <p><span>USD</span><span><span>$</span><span></span></span></p>
            </div>
    ';
}

I have my $qty[] array for each cart item, but it only works if both items are in the cart. I can't figure out how to organize them so that each qty has a unique value to its product during the loop so that no matter which item is being ordered, it is assigned to it's respective product name and not by the order in which it is being purchased.

The problem is that if there is no second item being added to the cart, it only reads the first value, and then my cart gets mismatched to my fixed 'name' values to 'quantity' values of whichever quantity there is to retrieve or comes first.

Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duangongqiong6958 2018-11-30 07:13
    关注

    Why don't you add a key to the $qty-array?

    // ...
    $qty[ $key ] = $values['product_quantity'];
    // ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 51寻迹小车定点寻迹
  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件