From 5448d85c1857ab151bc4402a43c6f62121a29d6e Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Sat, 4 Apr 2020 12:21:30 +0200 Subject: [PATCH] seperate class for dbconnection created class for Movie reindexing --- index.html | 1 + js/index.js | 11 +++++----- php/Database.php | 34 +++++++++++++++++++++++++++++++ php/Movie.php | 10 +++++++++ php/{movie.php => index.php} | 39 ++++++++++++++++++------------------ 5 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 php/Database.php create mode 100644 php/Movie.php rename php/{movie.php => index.php} (58%) diff --git a/index.html b/index.html index cb87d4a..f32081c 100644 --- a/index.html +++ b/index.html @@ -19,6 +19,7 @@
vid1
vid2
+ mylabel
diff --git a/js/index.js b/js/index.js index 9b4bea5..b96c075 100644 --- a/js/index.js +++ b/js/index.js @@ -1,5 +1,5 @@ -Req.ready((event) => { - Req.post("php/movie.php", "action=getMovies&test=2", function (dta) { +Req.ready(() => { + Req.post("php/index.php", "action=getMovies&test=2", function (dta) { console.log(dta); for (const ii in dta.data) { document.getElementsByClassName("mediawrapper").item(0).insertAdjacentHTML('beforeend', ` @@ -7,15 +7,16 @@ Req.ready((event) => { } }); - Req.post("php/movie.php", "action=storePic", function (dta) { + Req.post("php/index.php", "action=storePic", function (dta) { console.log(dta); }); - Req.post("php/movie.php", "action=readPic", function (dta) { + Req.post("php/index.php", "action=readPic", function (dta) { console.log(dta); + document.getElementById("tempici").src = dta.data; }); - Req.post("php/movie.php", "action=getDbSize", function (dta) { + Req.post("php/index.php", "action=getDbSize", function (dta) { console.log(dta); }); }); \ No newline at end of file diff --git a/php/Database.php b/php/Database.php new file mode 100644 index 0000000..a633e34 --- /dev/null +++ b/php/Database.php @@ -0,0 +1,34 @@ +conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname); + } + + public static function getInstance() + { + if (!self::$instance) { + self::$instance = new Database(); + } + + return self::$instance; + } + + public function getConnection() + { + return $this->conn; + } +} \ No newline at end of file diff --git a/php/Movie.php b/php/Movie.php new file mode 100644 index 0000000..1c7eb32 --- /dev/null +++ b/php/Movie.php @@ -0,0 +1,10 @@ +connect_error) { - echo('{"data":"error"}'); - return; -} +require 'Database.php'; +$conn = Database::getInstance()->getConnection(); if (isset($_POST['action'])) { $action = $_POST['action']; @@ -21,7 +10,7 @@ if (isset($_POST['action'])) { echo('{"data":[{"url":"./vid.mp4"}]}'); break; case "getDbSize": - $query = "SELECT table_schema AS \"Database\", + $query = "SELECT table_schema AS \"Database\", ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS \"Size\" FROM information_schema.TABLES WHERE TABLE_SCHEMA='test' @@ -31,26 +20,36 @@ if (isset($_POST['action'])) { if ($result->num_rows == 1) { $row = $result->fetch_assoc(); - echo '{"data":"'.$row["Size"].'"}'; + echo '{"data":"' . $row["Size"] . '"}'; } break; case "storePic": // Select file type - $imageFileType = strtolower(pathinfo("./lukas.png",PATHINFO_EXTENSION)); + $imageFileType = strtolower(pathinfo("./lukas.png", PATHINFO_EXTENSION)); // Convert to base64 - $image_base64 = base64_encode(file_get_contents("./lukas.png") ); - $image = 'data:image/'.$imageFileType.';base64,'.$image_base64; + $image_base64 = base64_encode(file_get_contents("./lukas.png")); + $image = 'data:image/' . $imageFileType . ';base64,' . $image_base64; // Insert record - $query = "insert into temppic(data) values('".$image."')"; + $query = "insert into temppic(data) values('" . $image . "')"; if ($conn->query($query) === TRUE) { echo('{"data":"successfully created entry"}'); } else { - echo('{"data":"'.$conn->error.'"}'); + echo('{"data":"' . $conn->error . '"}'); } + break; + case "readPic": + $query = "SELECT * FROM temppic WHERE id='5'"; + + $result = $conn->query($query); + $row = $result->fetch_assoc(); + + echo('{"data":"' . $row["data"] . '"}'); + break; } } else { echo('{"data":"error"}'); } $conn->close(); +return;