【问题标题】:Can not get OOP to work correctly - Shopping Cart, PHP无法让 OOP 正常工作 - 购物车、PHP
【发布时间】: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


【解决方案1】:

你的函数有错误

    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”不是拼写错误,如果需要,应该将产品更改为产品。

【讨论】:

  • 对不起,我修正了那个拼写。翻译错了,它应该是$products = explode(",",$cart);,正如你所说,这就是我在原始代码中得到的,只有挪威语;但仍然无法正常工作!啊哈,是的,现在我知道“s”了。
  • 我添加了一张我得到的照片。
  • 很明显你的查询是个问题。没有指定$conn。你应该检查一下。
  • 我已经在另一个需要的 php-dokument 中指定了;但查询有问题。我更详细地找出了问题所在,因此我将删除此帖子并发布一个新的帖子,其中包含不起作用的代码行。
  • 你好 RST,我发现我需要在类内定义 $conn,而不仅仅是在类外......我不知道这是必要的!
【解决方案2】:

在这一行,$produksjon[] = '&lt;table';,标签没有正确关闭。

应该是$produksjon[] = '&lt;table&gt;';

【讨论】:

  • 它对显示代码的问题没有帮助,但它确实纠正了那个错误!
  • 你在说什么?那是您的代码中的错误。
  • 不是唯一一个。
  • 不是导致“东西”不显示的主要因素!
【解决方案3】:

我忘记在 ShoppingCart 类本身中定义 $conn。我只是在外面定义的!不知道这是必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-02
    • 2015-04-19
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 2016-08-05
    相关资源
    最近更新 更多