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

时间:2016-05-17 10:23:25

标签: php html mysql database

我有这样的东西,该功能允许我从“产品”表中显示数据库“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>

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

2 个答案:

答案 0 :(得分:2)

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

在while循环之前,

  echo '<table>';

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

 While(...){

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

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

  }

  echo '</table>

我希望你从这个

中选择理解

答案 1 :(得分:1)

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

首先。在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 *)模式,你会发现要求数据的视图不是最好的方法(拉动数据)。更好的方法是数据推送到视图。但现在这是另一个故事。

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

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

就是这样。我希望我能激励你拓宽你对最佳实践的看法。 :)