· 9 years ago · Jun 18, 2017, 07:34 PM
1<?php
2class simpleCMS{
3 public $host="localhost";
4 public $username="root";
5 public $password="";
6 public $db = 'testDB';
7
8
9 public function connectDB() {
10 $link = mysql_connect($this->host, $this->username, $this->password); //подключаемÑÑ Ðº Ñерверу MySQL
11 if (!$link) {
12 die('Ошибка ÑоединениÑ: ' . mysql_error());
13 }
14 mysql_select_db($this->db) or die("Ðе могу найти БД. " . mysql_error()); //подÑоеденÑем БД
15 $this->buildDB();//вызываем метод внутри другого
16 return $link;
17 }
18
19 public function buildDB() {
20 $sql = "CREATE TABLE IF NOT EXISTS `messages` (
21 `mid` int(11) NOT NULL,
22 `title` varchar(150) DEFAULT NULL,
23 `body` text,
24 `created` varchar(100) DEFAULT NULL,
25 PRIMARY KEY (`mid`)
26 ) ENGINE=InnoDB DEFAULT CHARSET=utf8";
27 $result = mysql_query($sql);
28 echo $result;
29 }
30
31 public function write($p) {
32 $sql = 'INSERT INTO messages(title, body, created) VALUES("' . $p["title"] . '", "' . $p["bodytext"] . '", ' . time() .')' or die(mysql_error());
33 return mysql_query($sql);
34 }
35
36 public function display_public() {
37 $content = '';
38
39 $sql = 'SELECT * FROM messages';
40 $result = mysql_query($sql);
41
42 while($row = mysql_fetch_array($result)) {
43 print '<div class="post">';
44 print '<span class="time">#' . $row['mid'] . 'от' .date('d-m-Y', $row['created']) . '</span><h2>' . $row['title'] . '</h2>';
45 print '<p>' . $row['bodytext']. '</p>';
46 print '</div>';
47 }
48 $content .= '<p><a href="/index.php?admin=1" > Добавить Ñообщение</a></p>';
49 return $content;
50 }
51
52
53
54}
55$obj = new simpleCMS;
56$db_connection = $obj->connectDB();
57$item = array("title" => "заголовок", "bodytext" => "текÑÑ‚");
58echo $obj->write($item);
59echo $obj->display_public();
60mysql_close($db_connection);
61?>