【问题标题】:Display data from database in a table在表中显示数据库中的数据
【发布时间】:2016-09-13 09:29:22
【问题描述】:

我有这样的东西,该功能允许我显示来自数据库“演示”的数据,来自“产品”表:

class Product
{
private $conn;
private $id;
private $name;
private $description;
private $price;
private $category_id;
private $category_name;
private $created;

public function __construct($db)
{
    $this->conn = $db;
}

public function readAll()
{
    $stmt = $this->conn->prepare('SELECT name, description, price, CategoryID, created FROM products');
    $stmt->execute();
    while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $n = $result['name'];
        $d = $result['description'];
        $p = $result['price'];
        $ca = $result['CategoryID'];
        $c = $result['created'];
        echo $n . " - " . $d . " - " . $p . " - " . $ca . " - " . $c . "<br />" . "<br />";
    }
}
}

这是我的数据库连接:

class Database {

public function getConnection() {
    $result = false;
    try {
        $result = new PDO('mysql:host=localhost;dbname=demo', 'root', '');
    } catch(PDOException $e) { }
    return $result;
}
}
$db = new Database();
$conn = $db->getConnection();
if (!$conn) {
die("Error connecting to the database");
}

我可以这样显示数据:

<div>
        <h3>readAll:</h3>
        <form action="" method="post">
            <label>Products: <br /> (Name - Description - Price - Category ID - Creation date): </label><br />
            <?php
            $cat = new Product($conn);
            echo $cat->readAll();
            ?>
        </form>
    </div>

但是如何在表格中显示数据库中的数据?

【问题讨论】:

  • 需要遍历数组,然后在循环中显示出来
  • 您可以使用 foreach 循环,因为您将数据作为数组获取。做一个 var_dump($cat->readAll());

标签: php html mysql database


【解决方案1】:

这里有一个线索,这完全取决于你的 php 的结构。

就在while循环之前,

  echo '<table>';

//如果您愿意,可以添加表头

 While(...){

    $a = $row['...'];

    echo '
     <tr><td>'.$a.'</td></tr>
    ';
   //you can add as many columns as you wish

  }

  echo '</table>

希望你能从中选择理解

【讨论】:

    【解决方案2】:

    这是实现您正在尝试的目标的简单方法。但在此之前,让我们先讨论一下您对正在构建的应用程序的处理方法。

    首先。在 php 代码中构造 html 不是一个好习惯。最好的方法是准备数据以供展示,并简单地向展示层提供这些数据(要更广泛地解释这种思维方式,请查看MVC pattern)。

    所以,让我们准备数据。在您获取产品的函数中,只需返回它们即可。

    public function getAll()
    {
        $stmt = $this->conn->prepare('SELECT name, description, price, CategoryID, created FROM products');
        $stmt->execute();
        $allProducts = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
        return $allProducts;
    }
    

    我还重命名了函数,因为对于我们要构建的内容,getAll()readAll() 更具描述性。

    现在,我们有一种快速简便的方法来获取我们需要的数据。

    让我们做演示吧。

    在你的视图 php 文件中,获取数据。

    <?php 
        // Get all products.
        $products = (new Product($conn))->getAll(); 
    ?>
    

    如果您深入研究 MVC(或任何 MV*)模式,您会发现请求数据的视图并不是最好的方法(提取数据)。更好的方法是将数据推送到视图。但现在这是另一回事了。

    所以,我们的产品位于 products 数组中。让我们迭代它。不要忘记首先检查是否有任何用户。

    <?php if ( ! empty($products)) : ?>
        <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Description</th>
                <th>Price</th>
                <th>CategoryID</th>
                <th>Created</th>
            </tr>
        </thead>
    
        <?php foreach ($products as $product) : ?>
            <tr>
                <td><?php $product['name'] ?></td>
                <td><?php $product['description'] ?></td>
                <td><?php $product['price'] ?></td>
                <td><?php $product['CategoryID'] ?></td>
                <td><?php $product['created'] ?></td>
            </tr>
        <?php endforeach; ?>
        </table>
    <?php endif; ?>
    

    就是这样。我希望我能启发您拓宽您对最佳实践的看法。 :)

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签