· 7 years ago · Mar 02, 2019, 06:20 AM
1class phase2 {
2 function __construct () {
3
4 $dbFile = 'dbconfig.php';
5 $this->dbFile = $dbFile;
6 include_once ("$this->dbFile");
7
8
9 $step = $_GET["step"];
10
11 $username = $DB_USER;
12 $password = $DB_PASS;
13 $server = $DB_SERVER;
14 $dbName = $DB_NAME;
15
16 $this->step = $step;
17 $this->dbFile = $dbFile;
18 $this->username = $username;
19 $this->password = $password;
20 $this->server = $server;
21 $this->dbName = $dbName;
22
23 $db = new PDO ('mysql:host=' .$server.';dbname='.$this->dbName,$this->username,$this->password);
24
25 $this->db = $db;
26
27 if (empty ($_GET['fot']) ) {
28 $fOT = 'false';
29 } elseif ($_GET['true']) { $fOT = 'true'; }
30
31 $this->IDB = $this->handleDatabase()->$IDB;
32 $this->IDB2 = $this->handleDatabase()->$IDB2;
33 $this->IDB3 = $this->handleDatabase()->$IDB3;
34 }
35
36
37
38public function handleDatabase (){
39// Prepare SQL Statements
40 $IDB = $this->db->prepare(
41 "CREATE TABLE pages (
42 id int(11) NOT NULL auto_increment,
43 subject_id int(11) NOT NULL,
44 menu_name varchar(30) NOT NULL,
45 position int(3) NOT NULL,
46 visible tinyint(1) NOT NULL,
47 content text NOT NULL,
48 PRIMARY KEY (id)
49 )ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8");
50
51 $IDB2 = $this->db->prepare("
52 CREATE TABLE subjects (
53 id int(11) NOT NULL auto_increment,
54 menu_name varchar(30) NOT NULL,
55 position int(3) NOT NULL,
56 visible tinyint(1) NOT NULL,
57 PRIMARY KEY (id)
58 )ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8");
59
60 $IDB3 = $this->db->prepare("
61 CREATE TABLE users (
62 id int(11) NOT NULL auto_increment,
63 username varchar(50) NOT NULL,
64 hashed_password varchar(40) NOT NULL,
65 PRIMARY KEY (id)
66 )ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8");
67}
68//Set Option to True or False
69
70function createTablePages ($fOT){
71
72 $r1 = $this->db->query('SHOW TABLES LIKE 'page'');
73 if (count($r1->fetchAll()) > 0 && $fOT === 'false') {
74 echo "The table PAGE exists";
75
76 } elseif ($fOT === 'true') {
77 $this->IDB->execute;
78 $this->stepFunction (1,false);
79 }
80}
81function createTableSubjects ($fOT){
82
83 $r2 = $this->db->query('SHOW TABLES LIKE 'subjects'');
84 if (count($r2->fetchAll()) > 0 && $fOT === 'false') {
85 echo "The table SUBJECTS exists ";
86
87 } elseif ($fOT === 'true') {
88
89 $this->IDB2->execute;
90 $this->stepFunction (2,false);
91
92 }
93}
94
95function createTableUsers ($fOT){
96
97 $r3 = $this->db->query('SHOW TABLES LIKE 'users'');
98 if (count($r3->fetchAll()) > 0 && $fOT === 'false') {
99 echo "The table USERS exists";
100 } elseif ($fOT === 'true') {
101 $this->IDB3->execute;
102 echo "Would you like to populate all the tables?";
103 }
104}
105
106
107public function stepFunction ($step, $fOT){
108
109switch ($step) {
110 case 0:
111 $this->createTablePages ($fOT);
112 break;
113 case 1:
114 $this->createTableSubjects($fOT);
115 break;
116 case 2: $this->createTableUsers ($fOT);
117 break;
118 }
119
120
121}
122
123 }
124
125} elseif ($fOT = 'true') {
126
127if (count($r1->fetchAll()) > 0 && $fOT = 'false') {
128
129if (count($r1->fetchAll()) > 0 && $fOT === 'false') {