繁体   English   中英

在表中显示数据库中的数据

[英]Display data from database in a table

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

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>

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

这是一个线索,这一切都取决于你的PHP结构。

就在while循环之前,

  echo '<table>';

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

 While(...){

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

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

  }

  echo '</table>

我希望你从中汲取理解

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

第一。 在PHP代码中构造html并不是一个好习惯。 您可以采用的最佳方法是准备用于演示的数据,并简单地为表示层提供此数据(有关此思维模式的更广泛解释,请查看MVC模式 )。

那么,让我们准备数据。 在您的功能中,只需返回产品即可获得产品。

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 *)模式,你会发现询问数据的视图不是最好的方法(拉动数据) 更好的方法是数据送到视图中。 但那是另一个故事。

所以,我们在产品阵列中有我们的产品 让我们迭代它。 不要忘记首先检查是否有任何用户。

<?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; ?>

而已。 我希望我能激励你拓宽你对最佳实践的看法。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM