[英]count(): Parameter must be an array or an object that implements Countable error in php
I am creating an add-to-cart system in my website.我正在我的网站上创建一个添加到购物车的系统。
If there are no items in the cart, the variable $cart
is set to NULL
.如果购物车中没有商品,则变量$cart
设置为NULL
。
This results in the following error when I try to echo the number of items in the cart: count(): Parameter must be an array or an object that implements Countable
.当我尝试回显购物车中的商品数量时,这会导致以下错误: count(): Parameter must be an array or an object that implements Countable
。
Here's my php code:这是我的 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
}
?>
This is because you are counting on 'NULL', Try it like this:这是因为你指望'NULL',试试这样:
.
.
.
else
{
$cart = [];
}
When $cart == NULL
(as it is assigned in your else
clause) it is not count
able since NULL
does not have a Countable
interface.当$cart == NULL
(因为它在你的else
子句中分配)时,它是count
的,因为NULL
没有Countable
接口。 As of PHP 7.2 , this results in the warning message you have seen.从PHP 7.2 开始,这会导致您看到警告消息。
But it seems you shouldn't try to output a cart when you don't have one, so you should just move that code inside your if
block ie但是,当您没有购物车时,您似乎不应该尝试 output 购物车,因此您应该将该代码移到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;
}
?>
It's because you're trying to count something that's not countable (how can you count the number of elements in null?).这是因为您试图计算不可数的东西(如何计算 null 中的元素数量?)。 From the documentation:从文档中:
7.2.0: count() will now yield a warning on invalid countable types passed to the array_or_countable parameter. 7.2.0:count() 现在将对传递给array_or_countable参数的无效可数类型产生警告。
so prior to version 7.2.0 this warning wouldn't be emitted.所以在版本 7.2.0 之前不会发出此警告。 In all versions, if obj
in count(obj)
isn't a valid array/countable object the function returns 1
, with the exception of count(null)
which returns 0
.在所有版本中,如果count(obj)
obj
的 obj 不是有效的数组/可计数 object ,则 function 返回1
,但count(null)
除外,它返回0
。
You can either:您可以:
1: <?php echo count((array)$cart);?>
1: <?php echo count((array)$cart);?>
2: else { $cart = []; }
2: else { $cart = []; }
else { $cart = []; }
3: <?php ($cart == null)? '': echo count($cart);?>
3: <?php ($cart == null)? '': echo count($cart);?>
<?php ($cart == null)? '': echo count($cart);?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.