【问题标题】:count(): Parameter must be an array or an object that implements Countable error in phpcount():参数必须是数组或对象,在php中实现Countable错误
【发布时间】:2020-08-15 23:02:00
【问题描述】:

我正在我的网站中创建一个添加到购物车的系统。

如果购物车中没有商品,则变量$cart 设置为NULL

当我尝试回显购物车中的商品数量时,这会导致以下错误:count(): Parameter must be an array or an object that implements Countable

这是我的 php 代码:

<?php 
if ((isset($_SESSION['active_user_type']) && $_SESSION['active_user_type'] == "consumer") || !isset($_SESSION['active_user'])) {
?>
<div class="shopping_cart">
<div class="cart_title">
    <a href="view_cart.php">Shopping cart</a>
</div>

<?php
    $total = 0;
    if(isset($_SESSION['cart'])) {
        $cart = $_SESSION['cart'];
        for ($i=0; $i<count($cart); $i++) {
            $item_id = $cart[$i][0];
            $query = "SELECT * FROM items WHERE id=$item_id";
            $result = $db->query($query);
            if ($row = $result->fetch()) {
                $price = ($row['price']*$cart[$i][1]) + $row['shipping_price'];
            }
            $total += $price;
        }
    } else {
        $cart = NULL;
    }
?>
<div class="cart_details">
// the error seems to be from the line below:
    <?php echo count($cart); ?><br />
    <span class="border_cart"></span> Total: 
    <span class="price">
        <?php echo "BD " . number_format((float)$total,3,'.',''); ?>
    </span>
</div>
<div class="cart_icon">
    <a href="checkout.php" title="Checkout">
        <img src="images/shoppingcart.png" alt="" width="48" height="48" border="0" />
    </a>
</div>
</div>
<?php
}
?>

【问题讨论】:

    标签: php html


    【解决方案1】:

    $cart == NULL(在您的else 子句中分配)时,它不是countable,因为NULL 没有Countable 接口。从PHP 7.2 开始,这会导致您看到警告消息。

    但您似乎不应该在没有购物车时尝试输出购物车,因此您应该将该代码移到 if 块中,即

    $total = 0;
    if(isset($_SESSION['cart']))
    {
        $cart = $_SESSION['cart'];
        for ($i=0; $i<count($cart); $i++)
        {
            $item_id = $cart[$i][0];
            $query = "SELECT * FROM items WHERE id=$item_id";
            $result = $db->query($query);
            if ($row = $result->fetch())
            {
                $price = ($row['price']*$cart[$i][1]) + $row['shipping_price'];
            }
            $total += $price;
        }
    ?>
    <div class="cart_details"> <?php echo count($cart);?> <br />
        <span class="border_cart"></span> Total: <span class="price"><?php echo "BD " . 
      number_format((float)$total,3,'.',''); ?></span> </div>
    <div class="cart_icon"><a href="checkout.php" title="Checkout"><img src="images/shoppingcart.png" 
    alt="" width="48" height="48" border="0" /></a></div>
     </div>
    <?php
    }
    else
    {
        $cart = NULL;
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      这是因为您要计算不可数的东西(如何计算 null 中的元素数?)。来自the documentation:

      7.2.0:count() 现在将对传递给 array_or_countable 参数的无效可数类型产生警告。

      所以在版本 7.2.0 之前不会发出此警告。在所有版本中,如果count(obj) 中的obj 不是有效的数组/可数对象,则函数返回1,但count(null) 除外,它返回0


      您可以:

      1. 将其转换为数组
      2. 如果为空,则将其显式设置为空数组
      3. 在回显之前进行检查

      1: &lt;?php echo count((array)$cart);?&gt;

      2: else { $cart = []; }

      3: &lt;?php ($cart == null) ? '' : echo count($cart);?&gt;

      【讨论】:

      • +1 用于参考文档以及为解决问题提供的 3 个选项。这应该是公认的答案。
      【解决方案3】:

      这是因为你指望'NULL',试试这样:

      .
      .
      .
      else
      {
          $cart = [];
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-20
        • 1970-01-01
        • 2018-07-24
        • 1970-01-01
        • 2019-08-03
        • 2019-08-15
        • 2019-02-26
        • 2019-05-10
        • 1970-01-01
        相关资源
        最近更新 更多