有人可以帮助解决使用 PHP 匹配功能显示产品的语法错误吗?
Can someone please help solve a syntax error using PHP match function to display products?
我正在尝试通过从 mysql 数据库中提取来显示产品。该代码使用一个名为 getProductById 的函数从数据库中提取产品。想法是将产品与合适的价格匹配,然后使用函数 cartElement 显示产品。非常感谢您花时间提供帮助。
注意:使用 PHP 版本 7.3.11
Error Message
:
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /Library/WebServer/Documents/J_Renzo_Mobile_Website_Draft/cart.php on line 90
My code
:
$total = 0;
if(isset($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
foreach ($_SESSION['cart'] as $cartProduct) {
$product = $database->getProductById((int)$cartProduct['product_id']);
$price = match($cartProduct['option']) {
'exclusive' => $product['exclusive'],
'unlimited' => (int)$product['unlimited'],
'premium' => (int)$product['premium'],
default => (int)$product['basic'],
};
if (is_numeric($price)) {
$total += $price;
}
cartElement($product['product_image'], $product['product_name'],$price,$product['id']);
//break; I don't know if I need this...
}
} else {
echo "<h5>Cart is empty.</h5>";
}
Bind_param() statement giving errors
:
//get product by id from database...
public function getProductById(int $id): array{
//$stmt = $this->con->prepare('SELECT * FROM $tablename WHERE id = ?');
$sql = "SELECT * FROM Productdb WHERE id = ?";
$stmt = $this->con->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
修改下面的匹配语句
$price = match($cartProduct['option']) {
'exclusive' => $product['exclusive'],
'unlimited' => (int)$product['unlimited'],
'premium' => (int)$product['premium'],
default => (int)$product['basic'],
};
阅读类似的内容以获取等效功能作为示例
$price = call_user_func(function($cartProduct, $product) {
switch($cartProduct['option']??null) {
case 'exclusive': return $product['exclusive'];
case 'unlimited' : return (int)$product['unlimited'];
case 'premium' : return (int)$product['premium'];
default : return (int)$product['basic'];
}
}, $cartProduct, $product);
我正在尝试通过从 mysql 数据库中提取来显示产品。该代码使用一个名为 getProductById 的函数从数据库中提取产品。想法是将产品与合适的价格匹配,然后使用函数 cartElement 显示产品。非常感谢您花时间提供帮助。
注意:使用 PHP 版本 7.3.11
Error Message
:
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /Library/WebServer/Documents/J_Renzo_Mobile_Website_Draft/cart.php on line 90
My code
:
$total = 0;
if(isset($_SESSION['cart']) && count($_SESSION['cart']) > 0) {
foreach ($_SESSION['cart'] as $cartProduct) {
$product = $database->getProductById((int)$cartProduct['product_id']);
$price = match($cartProduct['option']) {
'exclusive' => $product['exclusive'],
'unlimited' => (int)$product['unlimited'],
'premium' => (int)$product['premium'],
default => (int)$product['basic'],
};
if (is_numeric($price)) {
$total += $price;
}
cartElement($product['product_image'], $product['product_name'],$price,$product['id']);
//break; I don't know if I need this...
}
} else {
echo "<h5>Cart is empty.</h5>";
}
Bind_param() statement giving errors
:
//get product by id from database...
public function getProductById(int $id): array{
//$stmt = $this->con->prepare('SELECT * FROM $tablename WHERE id = ?');
$sql = "SELECT * FROM Productdb WHERE id = ?";
$stmt = $this->con->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
return $result->fetch_assoc();
}
修改下面的匹配语句
$price = match($cartProduct['option']) {
'exclusive' => $product['exclusive'],
'unlimited' => (int)$product['unlimited'],
'premium' => (int)$product['premium'],
default => (int)$product['basic'],
};
阅读类似的内容以获取等效功能作为示例
$price = call_user_func(function($cartProduct, $product) {
switch($cartProduct['option']??null) {
case 'exclusive': return $product['exclusive'];
case 'unlimited' : return (int)$product['unlimited'];
case 'premium' : return (int)$product['premium'];
default : return (int)$product['basic'];
}
}, $cartProduct, $product);