Преглед изворни кода

Создание поста

Александр Мусихин пре 2 година
родитељ
комит
2f7bb5c120

+ 0 - 8
.idea/.gitignore

@@ -1,8 +0,0 @@
-# Default ignored files
-/shelf/
-/workspace.xml
-# Editor-based HTTP Client requests
-/httpRequests/
-# Datasource local storage ignored files
-/dataSources/
-/dataSources.local.xml

+ 0 - 8
.idea/MyProject.iml

@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module type="WEB_MODULE" version="4">
-  <component name="NewModuleRootManager">
-    <content url="file://$MODULE_DIR$" />
-    <orderEntry type="inheritedJdk" />
-    <orderEntry type="sourceFolder" forTests="false" />
-  </component>
-</module>

+ 0 - 28
.idea/deployment.xml

@@ -1,28 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="PublishConfigData" remoteFilesAllowedToDisappearOnAutoupload="false">
-    <serverData>
-      <paths name="8pay">
-        <serverdata>
-          <mappings>
-            <mapping local="$PROJECT_DIR$" web="/" />
-          </mappings>
-        </serverdata>
-      </paths>
-      <paths name="docker">
-        <serverdata>
-          <mappings>
-            <mapping local="$PROJECT_DIR$" web="/" />
-          </mappings>
-        </serverdata>
-      </paths>
-      <paths name="ftp://dth.myjino.ru:21">
-        <serverdata>
-          <mappings>
-            <mapping local="$PROJECT_DIR$" web="/" />
-          </mappings>
-        </serverdata>
-      </paths>
-    </serverData>
-  </component>
-</project>

+ 0 - 8
.idea/modules.xml

@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="ProjectModuleManager">
-    <modules>
-      <module fileurl="file://$PROJECT_DIR$/.idea/MyProject.iml" filepath="$PROJECT_DIR$/.idea/MyProject.iml" />
-    </modules>
-  </component>
-</project>

+ 0 - 19
.idea/php.xml

@@ -1,19 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="MessDetectorOptionsConfiguration">
-    <option name="transferred" value="true" />
-  </component>
-  <component name="PHPCSFixerOptionsConfiguration">
-    <option name="transferred" value="true" />
-  </component>
-  <component name="PHPCodeSnifferOptionsConfiguration">
-    <option name="highlightLevel" value="WARNING" />
-    <option name="transferred" value="true" />
-  </component>
-  <component name="PhpStanOptionsConfiguration">
-    <option name="transferred" value="true" />
-  </component>
-  <component name="PsalmOptionsConfiguration">
-    <option name="transferred" value="true" />
-  </component>
-</project>

+ 8 - 1
app/bootstrap.php

@@ -1,9 +1,16 @@
 <?php
 
+function dd($a)
+{
+    var_dump($a); die();
+}
+
+
 require_once ('core/controller.php');
 require_once ('core/model.php');
 require_once ('core/view.php');
 require_once ('core/route.php');
 require_once ('core/db.php');
 
-Route::start();
+Route::start();
+

+ 16 - 4
app/controllers/controller_main.php

@@ -1,7 +1,4 @@
 <?php
-
-
-
 class controller_main extends Controller
 {
     public function __construct()
@@ -10,11 +7,26 @@ class controller_main extends Controller
         if(empty($_SESSION['user'])){
             header('Location: /user/login');
         }
+        $this->model = new Model_main();
+
     }
 
     function index()
     {
-        $this->view->generate('main_view.php');
+        include 'app/models/model_user.php';
+        $users_model = new Model_user();
+        $data['users'] = $users_model->get_users();
+        $data['posts'] = $this->model->get_posts();
+
 
+        $this->view->generate('main_view.php', $data);
+
+    }
+
+    public function createpost(){
+        if(isset($_POST['caption'], $_POST['post'])){
+            $this->model->create_post($_POST['caption'], $_POST['post']);
+        }
+        header('Location: /');
     }
 }

+ 50 - 41
app/core/db.php

@@ -1,42 +1,51 @@
-<?php
-
-class db
-{
-    private $credinternals = [
-        'dbhost' => 'localhost',
-        'dbname' => 'myproject',
-        'dbuser' => 'root',
-        'dbpass' => ''
-    ];
-
-    public  $conn;
-
-    public function __construct(){
-
-
-        $this->conn = mysqli_connect(
-            $this->credinternals['dbhost'],
-            $this->credinternals['dbuser'],
-            $this->credinternals['dbpass'],
-            $this->credinternals['dbname']
-        );
-
-        if(!$this->conn) die('DB connect error!');
-
-    }
-
-    public function query($q){
-        $data = mysqli_query($this->conn, $q);
-        if(is_bool($data)) {
-            return $data;
-        } else {
-            return $data->fetch_assoc();
-        }
-
-    }
-
-    public function escape($str){
-        return "'" . mysqli_real_escape_string($this->conn, $str) . "'";
-    }
-
+<?php
+
+class db
+{
+    private $credinternals = [
+        'dbhost' => 'localhost',
+        'dbname' => 'myproject',
+        'dbuser' => 'root',
+        'dbpass' => ''
+    ];
+
+    public  $conn;
+
+    public function __construct(){
+
+
+        $this->conn = mysqli_connect(
+            $this->credinternals['dbhost'],
+            $this->credinternals['dbuser'],
+            $this->credinternals['dbpass'],
+            $this->credinternals['dbname']
+        );
+
+        if(!$this->conn) die('DB connect error!');
+
+    }
+
+    public function query($q){
+        $data = mysqli_query($this->conn, $q);
+        if(!$data) {
+            die('Ошибка базы данных: ' . mysqli_error($this->conn));
+        }
+
+
+        if(is_bool($data)) {
+            return $data;
+        } else {
+            $ret = [];
+            while($d = $data->fetch_assoc()){
+                $ret[] = $d;
+            }
+            return $ret;
+        }
+
+    }
+
+    public function escape($str){
+        return "'" . mysqli_real_escape_string($this->conn, $str) . "'";
+    }
+
 }

+ 9 - 0
app/core/model.php

@@ -2,7 +2,16 @@
 
 class model
 {
+    protected $db;
+
+    public function __construct()
+    {
+        $this->db = new db();
+    }
+
     public function get_data()
     {
+
+
     }
 }

+ 18 - 0
app/models/model_main.php

@@ -0,0 +1,18 @@
+<?php
+
+class Model_main extends Model
+{
+    public function get_posts(){
+        $q = "SELECT * FROM `posts` ORDER BY `updated_at` DESC";
+        return $this->db->query($q);
+    }
+
+    public function create_post($caption, $post){
+        $q = "INSERT INTO `posts` SET `caption` = " . $this->db->escape($caption) .
+            ", `post` = " . $this->db->escape($post) .
+            ", `user_id` = " . $this->db->escape($_SESSION['user']['id']);
+        $this->db->query($q);
+
+    }
+
+}

+ 24 - 24
app/models/model_user.php

@@ -1,25 +1,25 @@
-<?php
-
-/**
- * @property $db
- */
-class Model_user extends Model
-{
-    private $db;
-    public function __construct()
-    {
-        $this->db = new db();
-    }
-
-    public function register($login, $password){
-        // создаем пользователя в БД
-
-        $this->db->query("INSERT INTO users SET `login` = " . $this->db->escape($login) .
-            ", `password` = " . $this->db->escape($password)
-        );
-    }
-
-    public function get_user_by_login($login){
-        return $this->db->query("SELECT * FROM users WHERE `login` = " . $this->db->escape($login));
-    }
+<?php
+
+/**
+ * @property $db
+ */
+class Model_user extends Model
+{
+
+    public function register($login, $password){
+        // создаем пользователя в БД
+
+        $this->db->query("INSERT INTO users SET `login` = " . $this->db->escape($login) .
+            ", `password` = " . $this->db->escape($password)
+        );
+    }
+
+    public function get_user_by_login($login){
+        $user = $this->db->query("SELECT * FROM users WHERE `login` = " . $this->db->escape($login));
+        return $user[0];
+    }
+
+    public function get_users(){
+        return $this->db->query("SELECT * FROM users");
+    }
 }

+ 24 - 0
app/views/main_view.php

@@ -7,3 +7,27 @@
         <a href="/user/logout">Выход</a>
     </div>
 </div>
+
+<form action="/main/createpost" method="post">
+    <div class="row my-3">
+
+        <div class="col-12">
+            <a  data-bs-toggle="collapse" href="#createform" role="button" aria-expanded="false" aria-controls="collapseExample">
+                Создать пост
+            </a>
+            <div class="collapse" id="createform">
+                <input required minlength="3" class="form-control mt-3" type="text" name="caption" placeholder="Заголовок">
+                <textarea required minlength="10" class="form-control mt-3" name="post" placeholder="текст поста"></textarea>
+                <div class="text-end mt-3">
+                    <button class="btn btn-primary" type="submit">Создать</button>
+                </div>
+            </div>
+            <hr>
+        </div>
+    </div>
+</form>
+
+
+<?php
+var_dump($posts);
+?>

+ 9 - 3
app/views/template_view.php

@@ -15,9 +15,15 @@
                 <!-- end main block -->
             </div>
         </section>
-            <pre>
-                <? var_dump($_SESSION);?>
-            </pre>
+        <footer>
+            <div class="row mt-5 fs-6">
+                <div class="col-12">
+                    <pre>
+                        <? //var_dump($_SESSION);?>
+                    </pre>
+                </div>
+            </div>
+        </footer>
         <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ENjdO4Dr2bkBIFxQpeoTz1HIcje39Wm4jDKdf19U8gI4ddQ3GYNS7NTKfAdVQSZe" crossorigin="anonymous"></script>
     </body>
 </html>