【发布时间】:2017-02-18 20:47:15
【问题描述】:
我似乎在这个购物车中找不到任何故障,但它显示不正确。我看到了这个 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);
}
}
?>
【问题讨论】:
-
对不起,这个网站不适合发布一串代码并询问它有什么问题
-
另外,非英语的源代码通常是一个非常糟糕的主意。出于这个原因,这篇文章很好地展示了,即使你的翻译,你为任何不说你的语言的人创造了一个相当高的进入障碍来帮助你。 (或者,如果您的团队扩展并获得国际同事等)
-
同意。此外,使用您提供的“词典”翻译它甚至需要付出努力。没有人喜欢努力,我们是程序员。您为我们做的越简单,我们就越有可能提供帮助!
-
是的。我会将整个代码翻译成英文;对不起...作为一个新手,我不知道除了发布“代码块”之外还能做什么。我已经尝试分配找到错误,并希望使其工作,但不太了解编码(应该先正确了解更多信息,呵呵)。
标签: php mysql oop pdo shopping-cart