· 8 years ago · Dec 07, 2017, 04:56 PM
1<?php
2 // put your code here
3 class simplecms {
4 var $host;
5 var $username;
6 var $password;
7 var $table;
8
9 public function display_public() {
10
11 $q = "SELECT * FROM testDB ORDER BY created DESC LIMIT 3";
12 $r = mysql_query($q);
13
14 if ( $r !== false && mysql_num_rows($r) > 0 ) {
15 while ( $a = mysql_fetch_assoc($r) ) {
16 $title = stripslashes($a['title']);
17 $bodytext = stripslashes($a['bodytext']);
18
19 $entry_display .= <<<ENTRY_DISPLAY
20
21 <h2>$title</h2>
22 <p>
23 $bodytext
24 </p>
25
26ENTRY_DISPLAY;
27 }
28 } else {
29 $entry_display = <<<ENTRY_DISPLAY
30
31 <h2>This Page Is Under Construction</h2>
32 <p>
33 No entries have been made on this page.
34 Please check back soon, or click the
35 link below to add an entry!
36 </p>
37
38ENTRY_DISPLAY;
39 }
40 $entry_display .= <<<ADMIN_OPTION
41
42 <p class="admin_link">
43 <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
44 </p>
45
46ADMIN_OPTION;
47
48 return $entry_display;
49 }
50
51 public function display_admin() {
52
53 return <<<ADMIN_FORM
54
55 <form action="{$_SERVER['PHP_SELF']}" method="post">
56 <label for="title">Title:</label>
57 <input name="title" id="title" type="text" maxlength="150" />
58 <label for="bodytext">Body Text:</label>
59 <textarea name="bodytext" id="bodytext"></textarea>
60 <input type="submit" value="Create This Entry!" />
61 </form>
62
63ADMIN_FORM;
64 }
65
66 public function write($p) {
67
68 if ( $p['title'] )
69 $title = mysql_real_escape_string($p['title']);
70 if ( $p['bodytext'])
71 $bodytext = mysql_real_escape_string($p['bodytext']);
72 if ( $title && $bodytext ) {
73 $created = time();
74 $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";
75 return mysql_query($sql);
76 } else {
77 return false;
78 }
79 }
80
81 public function connect() {
82
83 mysql_connect($this->host,$this->username,$this->password) or die("Could not connect". mysql_error());
84 mysql_select_db($this->table) or die("could not select table" . mysql_error());
85
86 return $this->buildDB();
87 }
88
89 private function buildDB() {
90 $sql = <<<MySQL_QUERY
91 CREATE TABLE IF NOT EXISTS testDB (
92 title VARCHAR(150),
93 bodytext TEXT,
94 created VARCHAR(100)
95 )
96 MySQL_QUERY;
97
98 return mysql_query($sql);
99}
100
101 }
102?>