我似乎在这个购物车中找不到任何错误,但它显示不正确..我看过这个YouTube视频,并从那里尝试了代码(https://www.youtube.com/watch?v=SlwMBtx0YMQ),但它改为使用MySQL MYSQL PDO;所以我转而使用PDO。我从另一个页面回显该类以显示HTML代码。是的,我是新来的..
代码从挪威语翻译成英语:
<?php
class shoppingCart {
// This function works as it should it seems like
function writeShoppingCart() {
$cart = $_SESSION["cart"];
if (!$cart) {
return "0 products";
} else {
$products = explode(",",$cart);
$number = (count($products) > 1) ? "s" : "";
return "<a href='cart.php'>".count($products)." Products ".$number."";
}
}
// But this function is not working as it should (?)
function showCart() {
$cart = $_SESSION["cart"];
if ($cart) {
$products = explode(",",$cart);
$content = array();
// FAULT HERE MAYBE ?
foreach ($products as $product) {
$content[$product] = (isset($content[$product])) ? $content[$product]+1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="POST" id="cart"';
$output[] = '<table>'; // This was corrected by "jeyoung" (">")
// HERE ? $content .. I THINK
foreach ($content as $productID=>$qty) {
$sql = "SELECT * FROM Product WHERE ProductID = ".productID;
// FAULT HERE ....?
echo " WORKS HERE 1 "; // From here and down the code stops displaying ...
$result = $conn->query($sql);
echo " WORKS HERE 2 "; // This is just check-marks to make it easier to find the fault ..
$user_data = $result->fetch(PDO::FETCH_BOTH);
echo " WORKS HERE 3 ";
extract($user_data);
$output[] = '<tr>';
$output[] = '<td><a href="cart.php?action=delete&id='.$productID.'" > Delete </a></td>';
$output[] = '<td>'.$productName.'</td>';
$output[] = '<td>NOK '.$PriceNOK.',-</td>';
$output[] = '<td><input type="text" name="qty'.$productID.'" value='.$qty.' size="3" maxlength="3" /></td>';
$output[] = '<td> NOK '.($PriceNOK * $qty).',-</td>';
$totalCost += $PriceNOK * $qty;
$output[] = '</tr>';
echo " WORKS HERE 4 ";
}
$output[] = '</table>';
$output[] = '<p> Total: <strong>NOK '.$totalCost.',-</strong></p>'; // NOK = Norwegian Kroner (currency)
$output[] = '<button type="submit">Update Cart</button>';
$output[] = '</form>';
} else {
$output[] = '<p>Cart is empty.</p>'; // Cart empty
}
return join('',$output);
}
}
?>
答案 0 :(得分:0)
在此行$produksjon[] = '<table';
上,代码未正确关闭。
应为$produksjon[] = '<table>';
。
答案 1 :(得分:0)
您的功能出错
function showCart() {
$cart = $_SESSION["cart"];
if ($cart) {
$output = explode(",",$cart);
$content = array();
// FAULT HERE MAYBE ?
foreach ($products as $product) {
您的foreach
已使用$products
,但您没有此变量。您需要像在第一个函数中那样分配该变量。而不是
$output = explode(",",$cart);
使用
$products = explode(",",$cart);
代码开头的“s”不是拼写错误,如果需要,它应该将产品变成产品。
答案 2 :(得分:0)
我忘了在ShoppingCart类中定义$ conn。我只在外面定义了它!不知道这是必要的。