【问题标题】:how to show extract the cart total value and show in other pages?如何显示提取购物车总价值并显示在其他页面中?
【发布时间】:2013-08-14 02:15:38
【问题描述】:

我设置了会话 cart_array,并显示了变量 $dynamicTotal 内的总数,所有这些代码都在我的 cart.php 中;现在我想从我的 cart.php 中检索这个 $dynamicTotal 并在我的 index.php 中回显我如何检索这个特定的值?任何提示都会很棒。谢谢

我已经以艰难的方式解决了这个问题。我所做的只是检索计算cart_total 所需的最少代码并将该代码粘贴到其他页面的所有标题顶部并回显该值。粘贴下面的代码以供分享。

我相信有一种简单的方法可以做到这一点,例如,将 cartTotal 设置为特定的会话变量,然后从我未能这样做的所有页面中检索该值。

<?php
    session_start();
    /* Created by Adam Khoury @ www.developphp.com */
    // Connect to the MySQL database  
    include "storescripts/connect_to_mysqli.php";
   $cartTotal = "";
   $dynamicTotal= "";

    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
       $dynamicTotal="empty";
    } else {
     // Start the For Each loop
        $i = 0;
        foreach ($_SESSION["cart_array"] as $each_item) {
            $item_id = $each_item['item_id'];
            $sqlCommand = "SELECT * FROM products WHERE id='$item_id' LIMIT 1";
            $sql = mysqli_query($myConnection, $sqlCommand);
            while ($row = mysqli_fetch_array($sql)) {
                $product_name = $row["product_name"];
                $price = $row["price"];
                $details = $row["details"];
            }
            $pricetotal = $price * $each_item['quantity'];
            $cartTotal = $pricetotal + $cartTotal;
            setlocale(LC_MONETARY, "ms_MY");
            $pricetotal = money_format("%10.2n", $pricetotal);
            $i++;
        }
        setlocale(LC_MONETARY, "ms_MY");
        $cartTotal = money_format("%10.2n", $cartTotal);

        $dynamicTotal ="$cartTotal";
           }
    ?>  

【问题讨论】:

    标签: php dynamic session-variables shopping-cart


    【解决方案1】:

    已经解决了,把我的解决方案贴出来,我认为有更好的方法来做到这一点

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多