Создание PHP MVC приложения. Создание пагинации
Снова я выполняю просьбу одного из слушателей, который попросил реализовать пагинацию. Для пагинации понадбится SQL запрос с использованием LIMIT. Подробно процесс описан в видео.
Код урока (Utils.php)
<?php
/**
* 1-ая - 5 продуктов
* 2-ая - с 6 по 10 продукт
* 3-я - с 11 по 15 продукт
* 5*(2-1)+1 = 6
* 5*(3-1)+1 = 11
* 5*(4-1)+1 = 16
* LIMIT $perPage*($page-1)+1, $itemsCount
* 15 -> 3
* 1 - 0-5
* 2 - 6- 10
* 3 - 11- 15
*/
class Utils {
public function drawPager($totalItems, $perPage) {
$pages = ceil($totalItems / $perPage);
if(!isset($_GET['page']) || intval($_GET['page']) == 0) {
$page = 1;
} else if (intval($_GET['page']) > $totalItems) {
$page = $pages;
} else {
$page = intval($_GET['page']);
}
$pager = "<nav aria-label='Page navigation'>";
$pager .= "<ul class='pagination'>";
$pager .= "<li><a href='/products?page=1' aria-label='Previous'><span aria-hidden='true'>«</span> Начало</a></li>";
for($i=2; $i<=$pages-1; $i++) {
$pager .= "<li><a href='/products?page=". $i."'>" . $i ."</a></li>";
}
$pager .= "<li><a href='/products?page=". $pages ."' aria-label='Next'>Конец <span aria-hidden='true'>»</span></a></li>";
$pager .= "</ul>";
return $pager;
}
}
Код урока (config.php)
<?php
session_start();
$_SERVER['DOCUMENT_ROOT'] = "/var/www/u0016495/data/www/cabinet.kamil-abzalov.ru";
define("ROOT", $_SERVER['DOCUMENT_ROOT']);
define("CONTROLLER_PATH", ROOT. "/controllers/");
define("MODEL_PATH", ROOT. "/models/");
define("VIEW_PATH", ROOT. "/views/");
define("UPLOAD_DIR", ROOT. "/uploads/");
define("UTILS", ROOT. "/utils/");
require_once("db.php");
require_once("route.php");
require_once UTILS . "Utils.php";
require_once MODEL_PATH. 'Model.php';
require_once VIEW_PATH. 'View.php';
require_once CONTROLLER_PATH. 'Controller.php';
Routing::buildRoute();
Код урока (ProductsController.php)
<?php
class ProductsController extends Controller {
private $pageTpl = "/views/products.tpl.php";
private $productsPerPage = 5;
public function __construct() {
$this->model = new ProductsModel();
$this->view = new View();
$this->utils = new Utils();
}
public function index() {
if(!$_SESSION['user']) {
header("Location: /");
}
$allProducts = count($this->model->getAllProducts());
$totalPages = ceil($allProducts / $this->productsPerPage);
$this->makeProductPager($allProducts, $totalPages);
$pagination = $this->utils->drawPager($allProducts, $this->productsPerPage);
$this->pageData['pagination'] = $pagination;
$this->pageData['title'] = "Товары";
// $this->pageData['productsOnPage'] = $this->model->getAllProducts();
$this->view->render($this->pageTpl, $this->pageData);
if($_FILES) {
if($_FILES['csv']['type'] != 'text/csv' || $_FILES['csv']['type'] == '') {
$this->pageData['errors'] = "Ошибка! Возможно данный файл имеет некорректный формат";
} else {
if(move_uploaded_file($_FILES['csv']['tmp_name'],UPLOAD_FOLDER.$_FILES['csv']['name'])) {
$file = fopen(UPLOAD_FOLDER.$_FILES['csv']['name'], "r");
$row = 1;
while($data = fgetcsv($file, 200, ";")) {
if($row == 1) {
$row++;
continue;
} else {
$this->model->addFromCSV($data);
}
}
fclose($file);
$this->model->getAllProducts();
}
}
}
}
public function getProduct() {
if(!$_SESSION['user']) {
header("Location: /");
return;
}
if(!isset($_GET['id'])) {
echo json_encode(array("success" => false));
} else {
$productId = $_GET['id'];
$productInfo = json_encode($this->model->getProductById($productId));
echo $productInfo;
}
}
public function saveProduct() {
if(!$_SESSION['user']) {
header("Location: /");
return;
}
if(!isset($_POST['id']) || trim($_POST['name']) == '' || trim($_POST['price']) == '') {
echo json_encode(array("success" => false, "text" => "Ошибка обновления данных"));
} else {
$productId = $_POST['id'];
$productName = strip_tags(trim($_POST['name']));
$productPrice = strip_tags(trim($_POST['price']));
echo json_encode(array("success" => true, "text" => "Информация о товаре обновлена"));
}
}
public function addProduct() {
if(!$_SESSION['user']) {
header("Location: /");
return;
}
if(empty($_POST) || trim($_POST['productName']) == '' || trim($_POST['productPrice']) == '') {
echo json_encode(array("success" => false, "text" => "Не удалось добавить товар"));
} else {
$productName = strip_tags(trim($_POST['productName']));
$productPrice = strip_tags(trim($_POST['productPrice']));
$this->model->addProduct($productName, $productPrice);
echo json_encode(array("success" => true, "text" => "Новый товар добавлен"));
}
}
public function deleteProduct() {
if(!$_SESSION['user']) {
header("Location: /");
return;
}
if(empty($_POST) || !isset($_POST['id'])) {
echo json_encode(array("success" => false));
} else {
$productId = $_POST['id'];
if($this->model->deleteProduct($productId)) {
echo json_encode(array("success" => true, "text" => "Товар удален"));
} else {
echo json_encode(array("success" => false, "text" => "Ошибка удаления товара"));
}
}
}
public function makeProductPager($allProducts, $totalPages) {
if(!isset($_GET['page']) || intval($_GET['page']) == 0 || intval($_GET['page']) == 1 || intval($_GET['page']) < 0) {
$pageNumber = 1;
$leftLimit = 0;
$rightLimit = $this->productsPerPage; // 0-5
} elseif (intval($_GET['page']) > $totalPages || intval($_GET['page']) == $totalPages) {
$pageNumber = $totalPages; // 2
$leftLimit = $this->productsPerPage * ($pageNumber - 1); // 5 * (2-1) = 6
$rightLimit = $allProducts; // 8
} else {
$pageNumber = intval($_GET['page']);
$leftLimit = $this->productsPerPage * ($pageNumber-1); // 5* (2-1) = 6
$rightLimit = $this->productsPerPage; // 5 -> (6,7,8,9,10)
}
$this->pageData['productsOnPage'] = $this->model->getLimitProducts($leftLimit, $rightLimit);
}
}
?>
Код урока (ProductsModel.php)
<?php
class ProductsModel extends Model {
public function getAllProducts() {
$result = array();
$sql = "SELECT * FROM products";
$stmt = $this->db->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['id']] = $row;
}
return $result;
}
public function getLimitProducts($leftLimit, $rightLimit) {
$result = array();
$sql = "SELECT * FROM products LIMIT :leftLimit, :rightLimit";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":leftLimit", $leftLimit, PDO::PARAM_INT);
$stmt->bindValue(":rightLimit", $rightLimit, PDO::PARAM_INT);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$result[$row['id']] = $row;
}
return $result;
}
public function addFromCSV($data) {
$sql = "INSERT INTO products(name, price) VALUES(:name, :price)";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":name", $data[0], PDO::PARAM_STR);
$stmt->bindValue(":price", $data[1], PDO::PARAM_INT);
$stmt->execute();
}
public function getProductById($id) {
$result = array();
$sql = "SELECT * FROM products WHERE id = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result;
}
public function saveProductInfo($id, $name, $price) {
$sql = "UPDATE products
SET price = :price, name = :name
WHERE id = :id
";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":price", $price, PDO::PARAM_INT);
$stmt->bindValue(":name", $name, PDO::PARAM_STR);
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->execute();
return true;
}
public function addProduct($productName, $productPrice) {
$sql = "INSERT INTO products(name, price)
VALUES(:productName, :productPrice)
";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":productName", $productName, PDO::PARAM_STR);
$stmt->bindValue(":productPrice", $productPrice, PDO::PARAM_INT);
$stmt->execute();
return true;
}
public function deleteProduct($id) {
$sql = "DELETE FROM products WHERE id = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$count = $stmt->rowCount();
if($count > 0) {
return true;
} else {
return false;
}
}
}
?>
Отличный урок. Слегка модифицировав код — добавил в свой учебный проект. Спасибо!
Здравствуйте. Спасибо за комментарий