【问题标题】:Fatal error: Uncaught Error: Call to a member function getSum() on array in MobileShopee\Mobile Shopee\Templet\_cart-templet.php:78 Stack trace: #0致命错误:未捕获错误:调用 MobileShopee\Mobile Shopee\Templet\_cart-templet.php:78 中的数组上的成员函数 getSum() 堆栈跟踪:#0
【发布时间】:2023-03-18 02:47:01
【问题描述】:

我在我的代码中遇到了这个问题,我想在 _cart-templet.php 文件中计算小计部分的价格,但它给了我错误我在下面包含了这个错误的所有必要代码:

致命错误:未捕获的错误:调用 C:\xampp\htdocs\MobileShopee\Mobile Shopee\Templet_cart-templet.php:78 中的数组上的成员函数 getSum() 堆栈跟踪:#0 C:\xampp\htdocs \MobileShopee\Mobile Shopee\cart.php(10): include() #1 {main} 在 C:\xampp\htdocs\MobileShopee\Mobile Shopee\Templet_cart-templet.php 第 78 行抛出

主cart.php 文件:

<?php
ob_start();
//include header.php
include ('header.php');
?>
    <?php
    //include products.php
    include ('Templet/_cart-templet.php');

    //include top-sale.php
    include ('Templet/_new-phones.php');
    ?>

<?php
//include footer.php
include ('footer.php');
?>

这是我的 _cart-templet.php 文件:

<!----Shopping section---->
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    if (isset($_POST['delete-cart-submit'])){
        $deletedRecord = $cart->deleteCart($_POST['item_id']);
    }

    // save for later
    if (isset($_POST['wishlist-submit'])){
        $cart->saveForLater($_POST['item_id']);
    }
}
?>
<section id="cart" class="py-3 mb-5">
    <div class="container-fluid w-75">
        <h5 class="font-baloo font-size-20">Shopping Cart</h5>
        <!--  shopping cart items   -->
        <div class="row">
            <div class="col-sm-9">
                <?php
                foreach ($product->getData('cart') as $item) :
                    $cart = $product->getProduct($item['item_id']);
                    $subTotal[] = array_map(function ($item){
                        ?>
                        <!-- cart item -->
                        <div class="row border-top py-3 mt-3">
                            <div class="col-sm-2">
                                <img src="<?php echo $item['item_image'] ?? "./assests/img/products/1.png" ;?>" style="height: 120px;" alt="cart1" class="img-fluid">
                            </div>
                            <div class="col-sm-8">
                                <h5 class="font-baloo font-size-20"><?php echo $item['item_name'] ?? "Unknown"; ?></h5>
                                <small>by <?php echo $item['item_brand'] ?? "Brand"; ?></small>
                                <!-- product rating -->
                                <div class="d-flex">
                                    <div class="rating text-warning font-size-12">
                                        <span><i class="fas fa-star"></i></span>
                                        <span><i class="fas fa-star"></i></span>
                                        <span><i class="fas fa-star"></i></span>
                                        <span><i class="fas fa-star"></i></span>
                                        <span><i class="far fa-star"></i></span>
                                    </div>
                                    <a href="#" class="px-2 font-rale font-size-14">20,534 ratings</a>
                                </div>
                                <!-- End product rating -->
                                <!-- product qty -->
                                <div class="qty d-flex pt-2">
                                    <div class="d-flex font-rale w-25">
                                        <button class="qty-up border bg-light" data-id="<?php echo $item['item_id'] ?? 0?>"><i class="fas fa-angle-up"></i></button>
                                        <input type="text" data-id="<?php echo $item['item_id'] ?? 0?>" class="qty_input border px-2 w-100 bg-light" disabled value="1" placeholder="1">
                                        <button data-id="<?php echo $item['item_id'] ?? 0?>" class="qty-down border bg-light"><i class="fas fa-angle-down"></i></button>
                                    </div>
                                    <form method="post">
                                        <input type="hidden" value="<?php echo $item['item_id'] ?? 0; ?>" name="item_id">
                                        <button type="submit" name="delete-cart-submit" class="btn font-baloo text-danger px-3 border-right">Delete</button>
                                    </form>
                                    <button type="submit" class="btn font-baloo text-danger">Save for Later</button>
                                </div>
                                <!-- End product qty -->
                            </div>
                            <div class="col-sm-2 text-right">
                                <div class="font-size-20 text-danger font-baloo">
                                    $<span class="product_price"><?php echo $item['item_price'] ?? 0; ?></span>
                                </div>
                            </div>
                        </div>
                        <!-- End cart item -->
                        <?php
                        return $item['item_price'];
                    }, $cart); // closing array_map function
                endforeach;
                ?>
            </div>
            <!-- subtotal section-->
            <div class="col-sm-3">
                <div class="sub-total border text-center mt-2">
                    <h6 class="font-size-12 font-rale text-success py-3"><i class="fas fa-check"></i> Your order is eligible for FREE Delivery.</h6>
                    <div class="border-top py-4">
                        <h5 class="font-baloo font-size-20">Subtotal (<?php echo isset($subTotal) ? count($subTotal) : 0; ?> items):&nbsp; <span class="text-danger">$<span class="text-danger" id="deal-price"><?php echo isset($subTotal) ? $cart->getSum($subTotal) : 0; ?></span> </span> </h5>
                        <button type="submit" class="btn btn-warning mt-3">Proceed to Buy</button>
                    </div>
                </div>
            </div>
            <!-- !subtotal section-->
        </div>
        <!-- End shopping cart items   -->
    </div>
</section>
<!----End Shopping section---->

这是我的 Cart.php 文件:

 public function getSum($arr){
        if(isset($arr)){
            $sum = 0;
            foreach ($arr as $item){
                $sum += floatval($item[0]);
            }
            return sprintf('%.2f' , $sum);
        }
    }

这是我的functions.php文件:

<?php
//require MYSQL Connection
require ("database/DBController.php");
//require product class
require ("database/Product.php");
//require product class
require ("database/Blog.php");
//require cart class
require ("database/Cart.php");
//DB Controller
$db = new DBController();
//product object
$product = new Product($db);
$product_shuffle = $product->getData();
//blog object
$blog = new Blog($db);
//cart object
$cart = new Cart($db);

【问题讨论】:

  • 能否请您使用有效的代码格式更新您的问题以提高可读性。

标签: php mysql database


【解决方案1】:
$cart = $product - getProduct($item['item_id']);

<php echo isset($subTotal) ? $cart - getSum($subTotal) : 0; ?>

第一个$cart引用不一样 第二。第一个$cart 是一个变量,第二个是来自Cart 类的对象

【讨论】:

    【解决方案2】:

    $cart = $product->getProduct($item['item_id']);

    }, $cart); // 关闭 array_map 函数


    ===>更改代码中上述各行中的变量名称,例如 $cart1 或其他任何内容。


    $cart->getSum($subTotal)


    由于上述代码中的 $cart 被视为变量,因此您在这里调用的是变量而不是对象。 所以改变变量名来访问对象。

    【讨论】:

      【解决方案3】:

      &lt;?php echo isset($subTotal) ? $cart-&gt;getSum($subTotal) : 0; ?&gt;

      这个$cart 是对象。您已经创建了一个名为 $cart = new Cart($db); 的 Cart 对象,因此您不能使用与变量相同的名称!

      然后改变以下这些:

      在functions.php中$cart_1 = new Cart($db);

      在 _cart-templet.php 中: &lt;?php echo isset($subTotal) ? $cart_1-&gt;getSum($subTotal) : 0; ?&gt;

      试试这个!希望你能得到你所期望的:)

      【讨论】:

        猜你喜欢
        • 2021-09-04
        • 1970-01-01
        • 1970-01-01
        • 2021-04-18
        • 2021-09-16
        • 2018-09-10
        • 1970-01-01
        • 2017-07-16
        • 2020-10-04
        相关资源
        最近更新 更多