このショッピングカートには何の不具合も見当たりませんが、正しく表示されていません。このYouTubeビデオを見て、 https://www.youtube.com/watch?v=SlwMBtx0YMQ)が、MYSQL PDOの代わりにMySQLを持っていました。私は代わりにPDOに変換しました。 HTMLコードを表示するために別のページからクラスをエコーしています。コードはノルウェーから英語に翻訳され、これに新しいです..OOPを正常に動作させることができません - ショッピングカート、PHP
はい、I:この行で
<?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);
}
}
?>
申し訳ありませんが、このサイトはコードの不一致を投稿し、何が間違っているかを尋ねるのには適していません –
また、英語以外のソースコードは一般的に非常に悪い考えです。このポストはあなたの翻訳を美しく表現しているため、あなたの言語を話していない人があなたを助けるためにかなり高いバリアを作成します。 (あるいはあなたのチームが拡大して国際的な同僚のものなどを取得した場合) – Quant
合意。さらに、提供された「辞書」でこれを翻訳することも努力しています。誰も努力していない、私たちはプログラマーです。あなたが私たちのために作るのが簡単になればなるほど、私たちはもっと助けになります! – Pete