From 177e267e18445014bbe5a24ed111958fbcb1e722 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Sat, 4 Apr 2020 19:44:31 +0200 Subject: [PATCH] store pics from api in db --- js/BaseRequest.js | 15 +++++++++++++++ js/index.js | 36 ++++++++++++++++++------------------ php/Movie.php | 24 ++++++++++++++++++------ php/index.php | 4 ++-- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/js/BaseRequest.js b/js/BaseRequest.js index fb223f6..b93f097 100644 --- a/js/BaseRequest.js +++ b/js/BaseRequest.js @@ -14,6 +14,21 @@ class Req { xhttp.send(data); } + static postData(url, data, callback) { + const xhttp = new XMLHttpRequest(); + xhttp.open("POST",url,true); + xhttp.onload = function(){ + if (this.readyState == 4 && this.status == 200) { + callback(xhttp.responseText); + } + } + xhttp.onerror = function () { + console.log("error") + }; + xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); + xhttp.send(data); + } + static get(url, callback) { const xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function(){ diff --git a/js/index.js b/js/index.js index b96c075..c73bbf2 100644 --- a/js/index.js +++ b/js/index.js @@ -1,22 +1,22 @@ 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', ` -
${dta.data[ii].url}
`); // - } + // 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', ` + //
${dta.data[ii].url}
`); // + // } + // }); + // + // Req.post("php/index.php", "action=storePic", function (dta) { + // console.log(dta); + // }); + + console.log("startup") + Req.postData("php/index.php", "action=readPic", function (dta) { + document.getElementById("tempici").src = dta; }); - Req.post("php/index.php", "action=storePic", function (dta) { - console.log(dta); - }); - - Req.post("php/index.php", "action=readPic", function (dta) { - console.log(dta); - document.getElementById("tempici").src = dta.data; - }); - - Req.post("php/index.php", "action=getDbSize", function (dta) { - console.log(dta); - }); + // Req.post("php/index.php", "action=getDbSize", function (dta) { + // console.log(dta); + // }); }); \ No newline at end of file diff --git a/php/Movie.php b/php/Movie.php index 9ae481d..b117ef2 100644 --- a/php/Movie.php +++ b/php/Movie.php @@ -1,11 +1,12 @@ baseurl . "search/movie?api_key=" . $this->apikey . "&query=" . $i)); - if($reply->total_results == 0){ + if ($i != "." && $i != "..") { + $reply = json_decode(file_get_contents($this->baseurl . "search/movie?api_key=" . $this->apikey . "&query=" . pathinfo($i)['filename'])); + if ($reply->total_results == 0) { // no results found // todo maybe parse first pictures somehow - }else{ + } else { + echo json_encode($reply->results[0]); + $image_base64 = base64_encode(file_get_contents($this->posterbase . $reply->results[0]->poster_path)); + $image = 'data:image/jpeg;base64,' . $image_base64; + // Insert record + $conn = Database::getInstance()->getConnection(); + $query = "insert into Movie(name,url,poster) values('" . pathinfo($i)['filename'] . "','/data/$i','" . $image . "')"; + if ($conn->query($query) === TRUE) { + echo('{"data":"successfully created entry"}'); + } else { + echo('{"data":"' . $conn->error . '"}'); + } } - echo json_encode($reply); + } } } diff --git a/php/index.php b/php/index.php index 0a44d4b..478aeaf 100644 --- a/php/index.php +++ b/php/index.php @@ -39,12 +39,12 @@ if (isset($_POST['action'])) { } break; case "readPic": - $query = "SELECT * FROM temppic WHERE id='5'"; + $query = "SELECT * FROM Movie WHERE movieid='3'"; $result = $conn->query($query); $row = $result->fetch_assoc(); - echo('{"data":"' . $row["data"] . '"}'); + echo($row["poster"]); break; }