Currently interested in the conclusion of all articles.
For further transmission to the controller, and for issuing the view.
Code samples, or where it is possible to learn more about it ....
I have a class with methods for getting an array.
class Db implements IDbAccess { protected $host = "localhost"; protected $login = "root"; protected $password = "root"; protected $dataBase = "devBlog"; protected $conn; protected static $instance = null; private function __construct(){} private function __clone(){} private function __wakeup(){} public static function getInstance() { if(is_null(self::$instance)){ self::$instance = new static; } return self::$instance; } public function getConnection() { if(is_null($this->conn)){ $this->conn = new mysqli( $this->host, $this->login, $this->password, $this->dataBase ); } if(!$this->conn){ throw new DbException("Что-то пошло не так"); } return $this->conn; } public function query($sql) { return $this->getConnection()->query($sql); } public function fetchAll($sql) { $res = $this->query($sql); return $res->fetch_all(MYSQLI_ASSOC); } public function fetchObject($sql, $class) { $res = $this->query($sql); return $res->fetch_object($class); } } Then I create a child class:
class Article extends Model { public $article_id; public $header; public $content; public $owner; public $date; public $login; public $name; public static function getAll() { return self::getConnetcion()->fetchAll ( "SELECT a.article_id, a.content, a.header, a.owner, a.date, users.name, users.login FROM devBlog.Articles a INNER JOIN devBlog.users ON a.owner = users.user_id;" ); } /* * */ public static function getById($id) { return self::getConnetcion()->fetchObject ( "SELECT a.article_id, a.content, a.header, a.owner, a.date, users.name, users.login FROM devBlog.Articles a INNER JOIN devBlog.users ON a.owner = users.user_id WHERE a.article_id = $id;", Article::class ); } public static function create(){ return self::getConnetcion()->query ( "INSERT INTO Articles (header, content, owner) VALUE ('Some new text',\"a lot of new things\",2);" ); } public static function update($id){ return self::getConnetcion()->query( "UPDATE Articles SET header=\"xxx\", content=\"some text\", owner =\"1\" WHERE article_id = $id;" ); } public static function delete($id){ return self::getConnetcion()->query( "DELETE FROM devBlog.Articles WHERE article_id = $id;" ); } } Then I create a class object in the controller and "inkstiruyruyu", stopped at this, the first experience I can not find the material for further output, or just the book did not reach this material.
class ArticleController { protected $templateRoot = "views"; protected $action; public function run(){ $method ='action' . ucfirst($this->action); $this->$method; } //вывести статьи public function actionList(){ $model = new Article(); $articles = $model->getAll(); $this->renderTemplate('articleList',['articles' => $articles]); } //вывести статьи по одиночке public function actionArticleById(){ } //удалить статью public function actionDelete(){ } //создать public function actionCreate(){ } //изменить статью public function actionUpdate(){ } protected function renderTemplate($name, $args =[]){ extract($args); $templatePath = "{$_SERVER['DOCUMENT_ROOT']}/{$this->templateRoot}/{$name}.php"; require "$templatePath"; } public function setAction($action) { $this->action = $action; return $this; } } The controller also uses the template articleList.php to issue a view in html.
<?php foreach ($articles as $article):?> <div> <h2> <a href="/index.php/?c=Article&a=Display&id=<? =$article['article_id'] ?>"> <?= $article['header'];?> </a> </h2 > <p><?=$article['content'];?></p > </div >