From 45430cb23ed70ebde41955b92afab92c5c67d539 Mon Sep 17 00:00:00 2001 From: Varun Sankar <37424416+sivav1@users.noreply.github.com> Date: Sat, 7 Jul 2018 17:11:44 +1200 Subject: [PATCH 01/10] primary installation configuration --- config.json | 1 + install/config.php | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 config.json create mode 100644 install/config.php diff --git a/config.json b/config.json new file mode 100644 index 0000000..5233996 --- /dev/null +++ b/config.json @@ -0,0 +1 @@ +{"isInstalled":false,"db_host":"localhost","db_username":"root","db_password":"password","db_name":"mytable"} \ No newline at end of file diff --git a/install/config.php b/install/config.php new file mode 100644 index 0000000..e0a6534 --- /dev/null +++ b/install/config.php @@ -0,0 +1,41 @@ +loadConfig(); + } + + public function loadConfig() { + $string = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/catchnz-test/config.json'); + $json_a = json_decode($string, true); + + $this->isInstalled = $json_a['isInstalled']; + $this->db_host = $json_a['db_host']; + $this->db_username = $json_a['db_username']; + $this->db_password = $json_a['db_password']; + $this->db_name = $json_a['db_name']; + } + + public function toJson(){ + return json_encode( + array( + 'isInstalled'=> $this->isInstalled, + 'db_host'=> $this->db_host, + 'db_username'=> $this->db_username, + 'db_password'=> $this->db_password, + 'db_name'=> $this->db_name + )); + } + + public function saveConfig(){ + $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/catchnz-test/config.json', 'w'); + fwrite($fp, $this->toJson()); + fclose($fp); + } +} +?> \ No newline at end of file From 5a3f188e9098630414b4242c45a138b28fa02db2 Mon Sep 17 00:00:00 2001 From: Varun Sankar <37424416+sivav1@users.noreply.github.com> Date: Sat, 7 Jul 2018 17:12:06 +1200 Subject: [PATCH 02/10] db installation forms and process --- install/form_submit.js | 18 +++++++++++++++++ install/form_submit.php | 24 ++++++++++++++++++++++ install/index.html | 44 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 install/form_submit.js create mode 100644 install/form_submit.php create mode 100644 install/index.html diff --git a/install/form_submit.js b/install/form_submit.js new file mode 100644 index 0000000..4125941 --- /dev/null +++ b/install/form_submit.js @@ -0,0 +1,18 @@ +$(document).ready(function(){ + $('#submit').click(function(){ + var json = { + 'isInstalled': false, + 'db_host': $('#inputHost').val(), + 'db_username': $('#inputUsername').val(), + 'db_password': $('#inputPassword').val(), + 'db_name': $('#inputDBname').val() + } + $.post('form_submit.php',json) + .done(function(data) { + console.log(data); + window.location = '../index.php' + }).fail(function(jqxhr, settings, ex) { + console.log('failed, ' , jqxhr); + }); + }) +}) \ No newline at end of file diff --git a/install/form_submit.php b/install/form_submit.php new file mode 100644 index 0000000..10ad631 --- /dev/null +++ b/install/form_submit.php @@ -0,0 +1,24 @@ +isInstalled = false;//$_POST['isInstalled']; +$config->db_host = $_POST['db_host']; +$config->db_username = $_POST['db_username']; +$config->db_password = $_POST['db_password']; +$config->db_name = $_POST['db_name']; + +$config->saveConfig(); + +if(!$config->isInstalled){ + $conn = mysqli_connect($config->db_host, $config->db_username, $config->db_password); + if (!$conn) { + die("Connection failed: " . mysqli_connect_error()); + } + $sql = "CREATE DATABASE " . $config->db_name; + if (mysqli_query($conn, $sql)) { + mysqli_close($conn); + echo 'success'; + } else { + die "Error creating database: " . mysqli_error($conn); + } + } \ No newline at end of file diff --git a/install/index.html b/install/index.html new file mode 100644 index 0000000..a0f4f51 --- /dev/null +++ b/install/index.html @@ -0,0 +1,44 @@ + +
+ + + + + + + + + + + + \ No newline at end of file From 7345132189e67d1ced16210b9e9cc4d7c53ed7cd Mon Sep 17 00:00:00 2001 From: Varun Sankar <37424416+sivav1@users.noreply.github.com> Date: Sat, 7 Jul 2018 17:12:29 +1200 Subject: [PATCH 03/10] database connection class --- db.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 db.php diff --git a/db.php b/db.php new file mode 100644 index 0000000..378a9c9 --- /dev/null +++ b/db.php @@ -0,0 +1,63 @@ +connect(); + } + + public function connect(){ + if(!$this->isConnected) + { + $this->conn = @mysql_connect($this->db_host,$this->db_username,$this->db_password); + if($this->conn) + { + $selectdb = @mysql_select_db($this->db_name,$this->conn); + if($selectdb) + { + $this->isConnected = true; + return true; + } else + { + return false; + } + } else + { + return false; + } + } else + { + return true; + } + } + + public function isTableExists($tableName){ + $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$tableName.'"'); + if($tablesInDb) + { + if(mysql_num_rows($tablesInDb) == 1) + { + return true; + } + else + { + return false; + } + } + } + + public function create($tablename, $columns){ + $query = "create table if not exists $table ($columns);"; + $res = @mysql_query($query, $this->conn) or die(mysql_error()); + if($this->isTableExists($tableName)){ + return true; + } else { + return false; + } + } + +} +?> \ No newline at end of file From 01ef7ad65202c1bd52c832484c6584a6557ade9c Mon Sep 17 00:00:00 2001 From: Varun Sankar <37424416+sivav1@users.noreply.github.com> Date: Sat, 7 Jul 2018 17:14:52 +1200 Subject: [PATCH 04/10] Model Class --- db.php | 2 +- install/config.php | 4 ++-- install/form_submit.php | 2 +- model/Model.php | 51 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 model/Model.php diff --git a/db.php b/db.php index 378a9c9..ea3b868 100644 --- a/db.php +++ b/db.php @@ -1,5 +1,5 @@ isInstalled = $json_a['isInstalled']; @@ -33,7 +33,7 @@ public function toJson(){ } public function saveConfig(){ - $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/catchnz-test/config.json', 'w'); + $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/config.json', 'w'); fwrite($fp, $this->toJson()); fclose($fp); } diff --git a/install/form_submit.php b/install/form_submit.php index 10ad631..79ba464 100644 --- a/install/form_submit.php +++ b/install/form_submit.php @@ -1,5 +1,5 @@ isInstalled = false;//$_POST['isInstalled']; $config->db_host = $_POST['db_host']; diff --git a/model/Model.php b/model/Model.php new file mode 100644 index 0000000..a112717 --- /dev/null +++ b/model/Model.php @@ -0,0 +1,51 @@ +result = null; + $q = 'SELECT '.$rows.' FROM '.$table; + if($where != null) + $q .= ' WHERE '.$where; + if($order != null) + $q .= ' ORDER BY '.$order; + if($this->isTableExists($table)) + { + $query = @mysql_query($q); + if($query) + { + $this->numResults = mysql_num_rows($query); + for($i = 0; $i < $this->numResults; $i++) + { + $r = mysql_fetch_array($query); + $key = array_keys($r); + for($x = 0; $x < count($key); $x++) + { + if(!is_int($key[$x])) + { + if(mysql_num_rows($query) > 1) + $this->result[$i][$key[$x]] = $r[$key[$x]]; + else if(mysql_num_rows($query) < 1) + $this->result = null; + else + $this->result[$key[$x]] = $r[$key[$x]]; + } + } + } + return true; + } + else + { + return false; + } + } + else + return false; + } +} +?> \ No newline at end of file From 64c253c6c22c1408b33d8e3645afb9cec85ad9f5 Mon Sep 17 00:00:00 2001 From: Varun Sankar <37424416+sivav1@users.noreply.github.com> Date: Sat, 7 Jul 2018 17:24:00 +1200 Subject: [PATCH 05/10] CustomerModel class --- model/CustomerModel.php | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 model/CustomerModel.php diff --git a/model/CustomerModel.php b/model/CustomerModel.php new file mode 100644 index 0000000..4e7f9d1 --- /dev/null +++ b/model/CustomerModel.php @@ -0,0 +1,36 @@ +db = $db; + } + + public function createTable(){ + $query = "CREATE TABLE IF NOT EXISTS `" . $this->table . "` (\n" + . " `id` int(11) NOT NULL,\n" + . " `first_name` varchar(20) NOT NULL,\n" + . " `last_name` varchar(20) NOT NULL,\n" + . " `email` varchar(100) NOT NULL,\n" + . " `gender` varchar(7) NOT NULL,\n" + . " `ip_address` varchar(23) NOT NULL,\n" + . " `company` varchar(100) NOT NULL,\n" + . " `city` varchar(50) NOT NULL,\n" + . " `title` varchar(100) NOT NULL,\n" + . " `website` text NOT NULL\n" + . ")"; + + $res = @mysql_query($query, $this->conn) or die(mysql_error()); + if($this->isTableExists($this->table)){ + return true; + } else { + return false; + } +} +} +?> \ No newline at end of file From 1dd497e38fc0ecf6123b19a7082c3053b9d6364b Mon Sep 17 00:00:00 2001 From: Varun Sankar <37424416+sivav1@users.noreply.github.com> Date: Sat, 7 Jul 2018 19:53:16 +1200 Subject: [PATCH 06/10] app install directory --- config.json | 2 +- db.php | 25 +++++++++------- install/config.php | 13 +++++++++ install/form_submit.js | 25 ++++++++++------ install/form_submit.php | 4 ++- install/index.html | 44 ---------------------------- install/index.php | 64 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 112 insertions(+), 65 deletions(-) delete mode 100644 install/index.html create mode 100644 install/index.php diff --git a/config.json b/config.json index 5233996..9008ca2 100644 --- a/config.json +++ b/config.json @@ -1 +1 @@ -{"isInstalled":false,"db_host":"localhost","db_username":"root","db_password":"password","db_name":"mytable"} \ No newline at end of file +{"isInstalled":false,"db_host":"","db_username":"","db_password":"","db_name":""} \ No newline at end of file diff --git a/db.php b/db.php index ea3b868..062728b 100644 --- a/db.php +++ b/db.php @@ -1,5 +1,10 @@ connect(); } + // mysql connecting method + /** + * connect to mysql db with the given parameters + */ public function connect(){ if(!$this->isConnected) { @@ -34,6 +43,11 @@ public function connect(){ } } + // table existance checking ... + /** + * Lchecks if table exists in database + * @param {?string} tableName - name of table to check + */ public function isTableExists($tableName){ $tablesInDb = @mysql_query('SHOW TABLES FROM '.$this->db_name.' LIKE "'.$tableName.'"'); if($tablesInDb) @@ -48,16 +62,5 @@ public function isTableExists($tableName){ } } } - - public function create($tablename, $columns){ - $query = "create table if not exists $table ($columns);"; - $res = @mysql_query($query, $this->conn) or die(mysql_error()); - if($this->isTableExists($tableName)){ - return true; - } else { - return false; - } - } - } ?> \ No newline at end of file diff --git a/install/config.php b/install/config.php index 25de27e..b985a8e 100644 --- a/install/config.php +++ b/install/config.php @@ -1,4 +1,9 @@ loadConfig(); } + // loads config from file ... + /** + * loads config from file + */ public function loadConfig() { $string = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/config.json'); $json_a = json_decode($string, true); @@ -32,6 +41,10 @@ public function toJson(){ )); } + // saves cofig into file ... + /** + * saves config into file + */ public function saveConfig(){ $fp = fopen($_SERVER['DOCUMENT_ROOT'] . '/catchnz_test/config.json', 'w'); fwrite($fp, $this->toJson()); diff --git a/install/form_submit.js b/install/form_submit.js index 4125941..6353711 100644 --- a/install/form_submit.js +++ b/install/form_submit.js @@ -1,5 +1,14 @@ -$(document).ready(function(){ - $('#submit').click(function(){ + // ... + /** + * calls on submit button click + */ +$(document).ready(function () { + $('#btnSubmit').click(function () { + if($('#inputHost').val().lenght <= 0 || $('#inputUsername').val().length <= 0 || + $('#inputPassword').val().length <= 0 || $('#inputDBname').val().length <= 0) { + alert("Please fill all fields..!!"); + return; + } var json = { 'isInstalled': false, 'db_host': $('#inputHost').val(), @@ -7,12 +16,12 @@ $(document).ready(function(){ 'db_password': $('#inputPassword').val(), 'db_name': $('#inputDBname').val() } - $.post('form_submit.php',json) - .done(function(data) { + $.post('form_submit.php', json) + .done(function (data) { console.log(data); window.location = '../index.php' - }).fail(function(jqxhr, settings, ex) { - console.log('failed, ' , jqxhr); + }).fail(function (jqxhr, settings, ex) { + console.log('failed, ', jqxhr); }); - }) -}) \ No newline at end of file + }); +}); \ No newline at end of file diff --git a/install/form_submit.php b/install/form_submit.php index 79ba464..31cd623 100644 --- a/install/form_submit.php +++ b/install/form_submit.php @@ -17,8 +17,10 @@ $sql = "CREATE DATABASE " . $config->db_name; if (mysqli_query($conn, $sql)) { mysqli_close($conn); + $config->isInstalled = true; + $config->saveConfig(); echo 'success'; } else { - die "Error creating database: " . mysqli_error($conn); + echo "Error creating database: " . mysqli_error($conn); } } \ No newline at end of file diff --git a/install/index.html b/install/index.html deleted file mode 100644 index a0f4f51..0000000 --- a/install/index.html +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/install/index.php b/install/index.php new file mode 100644 index 0000000..2ebd1fc --- /dev/null +++ b/install/index.php @@ -0,0 +1,64 @@ +isInstalled){ + header('Location: ../index.php'); +} +?> + + + + + + + + + +| ID | +First Name | +Last Name | +Gender | +IP Address | +Company | +City | +Title | +Website | +
|---|