duangenshi9836 2015-02-26 08:07
浏览 16

我怎么回事这些数组来自db的实际值? 继续空白

So the ultimate goal is to print out products as objects in a table using functions from these classes, but the output is a table with the correct amount of rows/columns but they are blank. I've spent 3 hours trying to figure this out and I'm stumped. There's three files

Product.php

<?php

class Product
{
    protected $product_id;

    protected $product_name;

    protected $product_price;

    public function __construct($product_id,$product_name, $product_price = '')
    {
        $this->setProductID($product_id);
        $this->setProductName($product_name);
        $this->setProductPrice($product_price);

    }


    public function getProductID() {
        return $this->product_id;
    }
    public function getProductName() {
        return $this->product_name;
    }

    public function getProductPrice() {
        return $this->product_price;
    }

    public function setProductID($product_id)
    {
        $this->product_id = $product_id;
    }
    public function setProductName($product_name)
    {
        $this->product_name= $product_name;
    }

    public function setProductPrice($product_price)
    {
        $this->product_price= $product_price;
    }


}

ProductMapper.php

<?php

class ProductMapper
{
    public function getProducts()
    {
        $dbConn = getDbConnection();

        $stmt = $dbConn->prepare("SELECT * FROM product");
        $stmt->execute();

        $outArray = array();

        while ($row = $stmt->fetch()) {
            $outArray[] = new Product($row['product_id'], $row['product_name'], $row['product_cost']);
        }

        return $outArray;
    }
}

and Index.php which is where it is outputted.

<?php

require('classes/functions.php');
require('classes/Product.php');
require('classes/ProductMapper.php');

$productMapper = new ProductMapper();
$rows = $productMapper->getProducts();

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Product</title>


</head>
<body>


    <tbody>
    <?php

    foreach ($rows as $row) {
        echo "<tr>";
        echo "<td>{$row->getProductID($this)}</td>";
        echo "<td>{$row->getProductName($id)}</td>";
        echo "<td>{$row->getProductPrice($id)}</td>";
        echo "</tr>";
    }
    ?>
    </tbody>
</table>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dsmvqp3124 2015-02-26 11:39
    关注

    There are a few minor issues with the code that you provide which prevent it working. Do you have 'error_reporting' set error-reporting?

    index.php :

    • The 'getProduct' methods do not accept any parameters. So you should not call them with any parameters.

    ProductMapper.php :

    • for consistancy i renamed the 'product_cost' column to 'product_price'.
    • i injected the database connection in the constructor and made it a property. Makes available the connection for any future method.

    Some example output:

    pid01   name-pid01  23.34
    pid02   name-pid02  101.67
    

    Here are the new scripts...

    index.php

    require('classes/functions.php');
    require('classes/Product.php');
    require('classes/ProductMapper.php');
    
    $productMapper = new ProductMapper(getDbConnecton());
    $rows = $productMapper->getProducts();
    
    // note: the 'getProductID', 'getProductName' and 'getProductPrice' methods
    //       do not accept or need, any parameters.
    
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Product</title>
    
    </head>
    <body>
    
    <table>
        <tbody>
        <?php
    
        foreach ($rows as $row) {
            echo "<tr>";
            echo "<td>{$row->getProductID()}</td>";
            echo "<td>{$row->getProductName()}</td>";
            echo "<td>{$row->getProductPrice()}</td>";
            echo "</tr>";
        }
        ?>
        </tbody>
    </table>
    
    // ProductMapper.php
    class ProductMapper
    {
        /**
         * @var PDO
         */
        private $dbConn = null;
    
        public function __construct(PDO $pdo)
        {
            $this->dbConn = $pdo;
        }
    
        public function getProducts()
        {
            $dbConn = $this->dbConn;
    
            $stmt = $dbConn->prepare("SELECT * FROM product");
            $stmt->execute();
    
            $outArray = array();
    
            while ($row = $stmt->fetch()) {
                $outArray[] = new Product($row['product_id'], $row['product_name'], $row['product_price']);
            }
    
            return $outArray;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比