Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions application/config/db.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

return [
'host' => '',
'name' => '',
'user' => '',
'password' => '',
];
'host' => 'localhost',
'dbname' => 'DEV',
'user' => 'root',
'password' => '123456',
'port' => '3306',
'charset' => 'utf8',
];
4 changes: 2 additions & 2 deletions application/controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function addAction() {
}
$id = $this->model->postAdd($_POST);
if (!$id) {
$this->view->message('success', 'Ошибка обработки запроса');
$this->view->message('error', 'Ошибка обработки запроса');
}
$this->model->postUploadImage($_FILES['img']['tmp_name'], $id);
$this->view->message('success', 'Пост добавлен');
Expand Down Expand Up @@ -84,4 +84,4 @@ public function postsAction() {
];
$this->view->render('Посты', $vars);
}
}
}
6 changes: 4 additions & 2 deletions application/lib/Db.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class Db {

public function __construct() {
$config = require 'application/config/db.php';
$this->db = new PDO('mysql:host='.$config['host'].';dbname='.$config['name'].'', $config['user'], $config['password']);
$this->db = new PDO('mysql:host='.$config['host'].';dbname='.$config['dbname'].';port='.$config['port'].';charset='.$config['charset'], $config['user'], $config['password']);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
}

public function query($sql, $params = []) {
Expand Down Expand Up @@ -43,4 +45,4 @@ public function lastInsertId() {
return $this->db->lastInsertId();
}

}
}
3 changes: 1 addition & 2 deletions application/models/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ public function postValidate($post, $type) {

public function postAdd($post) {
$params = [
'id' => '',
'name' => $post['name'],
'description' => $post['description'],
'text' => $post['text'],
];
$this->db->query('INSERT INTO posts VALUES (:id, :name, :description, :text)', $params);
$this->db->query('INSERT INTO posts(name, description, text) VALUES (:name, :description, :text)', $params);
return $this->db->lastInsertId();
}

Expand Down
7 changes: 6 additions & 1 deletion application/models/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ public function postsCount() {

public function postsList($route) {
$max = 10;
if(isset($route['page']) && !empty($route['page'])){
$start = $route['page'];
} else {
$start = 0;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$start = ($start!=NULL)?$start:1; //кажется этой строки не хватает

$params = [
'max' => $max,
'start' => ((($route['page'] ?? 1) - 1) * $max),
'start' => ($start * $max),
];
return $this->db->row('SELECT * FROM posts ORDER BY id DESC LIMIT :start, :max', $params);
}
Expand Down