Reputation: 355
I've got something like this, the function allows me to display data from the database "demo", from "products" table:
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 />";
}
}
}
Here is my database connection:
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");
}
I can display data like this:
<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>
But how can I display data from the database in a table?
Upvotes: 1
Views: 225
Reputation: 526
Here's a simple way to achieve what you're trying. But before that, let's discuss a for a moment your approach to the application you're building.
First. It's not a good practice to construct the html in your php code. The best way you can go about it is to prepare the data for presentation and simply provide the presentation layer with this data (for a much broader explanation of this mindset, take a look at the MVC pattern).
So, let's prepare the data. In your function, where you get the products, simply return them.
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;
}
I've also renamed the function, because for what we're going to build, getAll() is a more descriptive than readAll().
Now, we have a quick and easy way to get the data we need.
Let's do the presentation.
In your view php file, get the data.
<?php
// Get all products.
$products = (new Product($conn))->getAll();
?>
If you dig deeper in the MVC (or any MV*) pattern, you'll find out that a view asking for the data is not the best approach (pulling the data). The better one is to push the data to the view. But that's another story now.
So, we have our products in the products array. Let's iterate over it. Don't forget to first check if there are any users at all.
<?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; ?>
That's it. I hope I inspired you to broaden your perspective on best practices. :)
Upvotes: 1
Reputation: 1142
Here's a clue, and it all depends on how your php is structured.
Just before the while loop,
echo '<table>';
// you can add table head if you please
While(...){
$a = $row['...'];
echo '
<tr><td>'.$a.'</td></tr>
';
//you can add as many columns as you wish
}
echo '</table>
I hope you picked understanding from this
Upvotes: 2