· 8 years ago · Oct 14, 2017, 07:34 AM
1<?php
2abstract class Model {
3
4 public function get_object_name()
5 {
6 return strtolower(get_class($this));
7 }
8
9 public function get_props()
10 {
11 $object_props = get_object_vars($this);
12 return $object_props;
13 }
14
15 public function create()
16 {
17 $object_props = $this->get_props();
18 $columns = "(";
19 $values = "(";
20 foreach($object_props as $key=>$value)
21 {
22 $columns.=$key.',';
23 $values.=$value.',';
24 }
25 $columns.=")";
26 $values.=")";
27
28 $query_string = "INSERT INTO ".$this->get_object_name()." ".$columns." VALUES ".$values;
29 echo $query_string;
30
31 }
32
33 public function delete()
34 {
35 $object_props = $this->get_props();
36 $columns = "(";
37 $values = "(";
38 foreach ($object_props as $key => $value) {
39 $columns.=$key.',';
40 $values.=$value.',';
41 }
42 $columns.=")";
43 $values.=")";
44
45 $query_string = "DELETE FROM ".$this->get_object_name()." "." WHERE ".$columns."=". $values;
46 echo $query_string;
47 }
48
49
50 public function update()
51 {
52 $object_props = $this->get_props();
53 $columns = "(";
54 $values = "(";
55 foreach ($object_props as $key => $value) {
56 $columns.=$key.',';
57 $values.=$value.',';
58 }
59 $columns.=")";
60 $values.=")";
61
62 $query_string = "UPDATE ".$this->get_object_name(). " SET ".$key. "=".$value." WHERE ".$columns." = ".$values;
63
64 echo $query_string;
65 }
66
67 public function select(){
68 $object_props = $this->get_props();
69 $columns = "(";
70 $values = "(";
71 foreach ($object_props as $key => $value) {
72 $columns.=$key.',';
73 $values.=$value.',';
74 }
75 $columns.=")";
76 $values.=")";
77 $query_string = "SELECT ".$columns. "FROM ".$this->get_object_name();
78 echo $query_string;
79 }
80
81
82 public function select_array(){
83 $object_props = $this->get_props();
84 $query_string = "SELECT * FROM ".$this->get_object_name()."WHERE ";
85
86 $items = array($columns =>$values);
87 $array_length = count($items);
88
89 foreach ($items as $key => $value) {
90 $index = array_search($key,array_keys($items));
91 $query_string.=$key.'='.$value;
92 if ($index!=$array_length-1) {
93 $query_string.='AND';
94 }
95
96 }
97 echo $query_string;
98 }
99}
100
101
102
103/*class Auth
104{
105 static public function authenticate($credentials = array('' => , );)(
106
107
108 )
109}*/
110
111class Db
112{
113 public $servername = 'localhost';
114 public $username = 'root';
115 public $password = 'passme@123';
116 public $dbname = 'testphp';
117
118 public function __construct()
119 {
120 $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname);
121 if($conn->connect_error)
122 {
123 die("could not connect". $conn->connect_error);
124 }
125 $this->conn = $conn;
126 return $this;
127 }
128
129 function query($query)
130 {
131 $this->conn->query($query);
132 }
133
134}
135
136/*function createUsers(){
137 $dbase = new Db;
138 $checkTable = "CREATE TABLE PostTable(
139 id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
140 firstname VARCHAR(30) NOT NULL,
141 messageBody VARCHAR(80) NOT NULL,
142 reg_date TIMESTAMP)";
143
144 if ($dbase->query($checkTable) === TRUE) {
145 echo "Table PostTable created successfully";
146 } else {
147 echo "Error creating table: " . $dbase->error;
148 }
149
150 if($checkTable !== FALSE){
151 echo "table already exists, moving on ";
152 }else{
153 return $checkTable;
154 }
155
156 $dbase->conn->close();
157 }*/
158
159class User extends Model {
160
161}
162
163?>