<?php
namespace components\plugins\menu;
use components\engine\DB;
class Menu
{
protected $db;
protected $data;
protected $tree;
protected $menuHtml;
protected $tpl = 'menu_tpl';
protected $view = 'menu';
protected $container = 'ul';
protected $class = 'menu';
protected $table = 'main_menu';
public function __construct($options = []){
$this->db = DB::instance();
$this->tpl = $_SERVER['DOCUMENT_ROOT'] . "/components/plugins/menu/{$this->tpl}/{$this->view}.php";
$this->getOptions($options);
$this->run();
}
protected function getOptions($options){
foreach($options as $k => $v){
if(property_exists($this, $k)){
$this->$k = $v;
}
}
}
protected function output(){
echo "<{$this->container} class='{$this->class}'>";
echo $this->menuHtml;
echo "</{$this->container}>";
}
protected function getCat(){
$res = $this->db->all("SELECT * FROM {$this->table}");
$cat = [];
foreach ($res as $row){
$cat[$row['id']] = $row;
}
return $cat;
}
protected function run(){
$this->data = $this->getCat();
$this->tree = $this->getTree();
$this->menuHtml = $this->getMenuHtml($this->tree);
$this->output();
}
protected function getTree(){
$tree = [];
$data = $this->data;
foreach ($data as $id => &$node) {
if (!$node['parent']){
$tree[$id] = &$node;
}else{
$data[$node['parent']]['childs'][$id] = &$node;
}
}
return $tree;
}
protected function getMenuHtml($tree, $tab = ''){
$str = '';
foreach($tree as $id => $category){
$str .= $this->catToTemplate($category, $tab, $id);
}
return $str;
}
protected function catToTemplate($category, $tab, $id){
ob_start();
require $this->tpl;
return ob_get_clean();
}
public function getAliasId($cat_alias){
if(!$cat_alias){
return false;
}
foreach ($this->data as $key => $value){
if($value['alias'] == $cat_alias){
return $key;
}
return false;
}
}
public function getParentId($id){
if(!$id){
return false;
}
$data = null;
foreach ($this->data as $cat){
if ($cat['parent'] == $id){
$data .= $cat['id'] . ",";
$data .= $this->getParentId($cat['id']);
}
}
return $data;
}
public function catalogIds($id_alias){
$get_id = $this->getAliasId($id_alias);
$get_parent_id = $this->getParentId($get_id);
if ($get_parent_id){
return $get_id;
}
return $get_parent_id.$get_id;
}
}
<li>
<a href="/catalog/<?=$category['alias'];?>/"><?=$category['title'];?></a>
<?php if(isset($category['childs'])): ?>
<ul>
<?= $this->getMenuHtml($category['childs']);?>
</ul>
<?php endif; ?>
</li>
<?php
namespace app\controllers;
use components\plugins\menu\Menu;
class CatalogController extends AppController
{
public function indexAction(){
$menu = new Menu();
$get_alias = $menu->getAliasId($this->route['alias']);
$get_id_parent = $menu->getParentId($get_alias);
$ids = $menu->catalogIds($get_alias);
$this->set(compact('get_cat_id','get_alias','get_id_parent','ids'));
}
}
function getImage($data,$folder = 'products'){
$path = ROOT . "/public/upload/images/{$folder}/{$data}.jpg";
if (file_exists($path)){
return $path;
}else{
return ROOT . "/public/upload/images/other/no-image.png";
}
}
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' `descr`, `content` WHERE id = ?' at line 1 in C:\OSPanel\domains\framework\components\engine\DB.php:31 Stack trace: #0 C:\OSPanel\domains\framework\components\engine\DB.php(31): PDO->prepare('UPDATE posts SE...') #1 C:\OSPanel\domains\framework\app\models\Post.php(34): components\engine\DB->query('UPDATE posts SE...', Array) #2 C:\OSPanel\domains\framework\app\controllers\admin\PostController.php(79): app\models\Post->updatePostById('2', '\xD0\x9A\xD0\xB0\xD0\xBA \xD1\x81\xD0\xBB\xD0\xB8\xD1\x82...', '\xD0\x9A\xD0\xB0\xD0\xBA \xD1\x81\xD0\xBB\xD0\xB8\xD1\x82...', '\xD0\x9A\xD0\xB0\xD0\xBA \xD1\x81\xD0\xBB\xD0\xB8\xD1\x82...') #3 C:\OSPanel\domains\framework\components\engine\Router.php(81): app\controllers\admin\PostController->updateAction() #4 C:\OSPanel\domains\framework\index.php(8): components in C:\OSPanel\domains\framework\components\engine\DB.php on line 31
public function registerAction(){
if(isset($_POST['submit'])){
$model = new Account;
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$errors = false;
if(!$model->checkName($name)){
$errors[] = 'Имя не должно быть короче 2-х символов';
}
if(!$model->checkEmail($email)){
$errors[] = 'Неправильный email';
}
if(!$model->checkPassword($password)){
$errors[] = 'Пароль не должен быть короче 6-ти символов';
}
if($model->checkEmailExists($email)){
$errors[] = 'Такой email уже исспользуется';
}
if ($errors == false){
$model->register($name,$email,$password);
header("Location: /");
}
}
var_dump($_POST);
$this->set(compact('errors','name','email','password'));
}
if($model->checkEmailExists($email)){
$errors[] = 'Такой email уже исспользуется';
}
public function checkEmailExists($email)
{
$params = [
'email' => $email
];
return $this->db->row('SELECT id FROM users WHERE email = :email', $params);
}
dependency injection