无法使 OOP 正常工作 - 购物车,PHP
Can not get OOP to work correctly - Shopping Cart, PHP
我似乎找不到这个购物车的任何问题,但它显示不正确。我看到了这个 YouTube 视频,并尝试了那里的代码 (https://www.youtube.com/watch?v=SlwMBtx0YMQ),但它有MySQL 而不是 MYSQL PDO;所以我改为转换为 PDO。我从另一个页面回显 class 以显示 HTML 代码。是的,我是新手..
The code is translated from Norwegian to English:
<?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);
}
}
?>
这一行,$produksjon[] = '<table';
,标签没有正确关闭。
应该是$produksjon[] = '<table>';
.
你的函数有错误
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"不是错字,需要的话应该把product改成products。
我忘记在 ShoppingCart-class 本身中定义 $conn。我只是在外面定义了它!不知道这是必要的。
我似乎找不到这个购物车的任何问题,但它显示不正确。我看到了这个 YouTube 视频,并尝试了那里的代码 (https://www.youtube.com/watch?v=SlwMBtx0YMQ),但它有MySQL 而不是 MYSQL PDO;所以我改为转换为 PDO。我从另一个页面回显 class 以显示 HTML 代码。是的,我是新手..
The code is translated from Norwegian to English:
<?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);
}
}
?>
这一行,$produksjon[] = '<table';
,标签没有正确关闭。
应该是$produksjon[] = '<table>';
.
你的函数有错误
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"不是错字,需要的话应该把product改成products。
我忘记在 ShoppingCart-class 本身中定义 $conn。我只是在外面定义了它!不知道这是必要的。