Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
//app/models/Product.php
require "/usr/local/www/apache24/data/config/Database.php";
class Product
{
private $db;
public function __construct() {
$this->db = new Database(); // Предполагается, что у вас есть класс для работы с БД
}
public function getProducts() {
$statement = $this->db->query("SELECT * FROM products");
return $this->db->resultSet($statement);
}
} < ?php
echo "app/controllers/ProductsController.php";
require "/usr/local/www/apache24/data/app/models/Product.php";
class ProductsController
{
private $productModel;
public function __construct() {
$this->productModel = new Product();
}
public function index() {
$products = $this->productModel->getProducts();
// передаем переменную $products в представление
include "/usr/local/www/apache24/data/app/views/products/index.php";
}
}
<!-- app/views/products/index.php -->
<!DOCTYPE html > < html lang = "en" > < head > < meta charset = "UTF-8" > < title > Список продуктов < /title > < link rel = "stylesheet" href = "/public/css/style.css" > < /head > < body > < div class = "container" > < h1 > Список продуктов < /h1 > < table > < thead > < tr > < th > ID < /th > < th > Название < /th > < th > Цена < /th > < th > Действия < /th > < /tr > < /thead > < tbody > < ?php foreach ($products as $product): ?>
<tr>
<td><?php echo $product['id']; ?></td>
<td><?php echo $product['name']; ?></td>
<td><?php echo $product['price']; ?> руб.</td>
<td>
<a href="/products/edit/<?php echo $product['id']; ?>">Редактировать</a> |
<a href="/products/delete/<?php echo $product['id']; ?>">Удалить</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</body>
</html>