Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Model
- {
- public $conn;
- public $con;
- public $testing;
- private $test;
- public function __construct(){
- include("Connect.php");
- $this->testing = "";
- $this->test = "";
- $this->conn = new con;
- $this->con = $this->conn->connect();
- }
- public function setTest($test){
- $this->test = $test;
- }
- public function getTest(){
- return $this->test;
- }
- public function testing(){
- $output= $this->testing = "hi there friend :)";
- return $output;
- }
- public function getArticles($obj){
- $this->setTest("");
- $newsub = json_decode($obj);
- $start = $newsub ->start;
- $perPage = $newsub ->pp;
- $articles = $this->con->prepare("
- SELECT SQL_CALC_FOUND_ROWS *
- FROM Article
- ORDER BY ArticleID DESC
- LIMIT ".$start.",".$perPage."
- ");
- $articles->execute();
- $fetch = array();
- while($forum = $articles->fetchAll(PDO::FETCH_ASSOC)){
- $fetch[] = $forum;
- }
- $test = json_encode($fetch);
- $this->setTest($test);
- //return json_encode($fetch);
- }
- public function mainart($obj){
- $this->setTest("");
- $newsub = json_decode($obj);
- $id = $newsub->id;
- $query = $this->con->prepare("
- SELECT *
- FROM Article, Main
- WHERE Main.Articleid = Article.ArticleID AND Main.ArticleID = :id
- ");
- $query ->bindParam(':id', $id);
- $query->execute();
- $fetch = array();
- while($forum = $query->fetchAll(PDO::FETCH_ASSOC)){
- $fetch[] = $forum;
- }
- $query2 = $this->con->prepare("
- SELECT *
- FROM Article
- WHERE ArticleID = :id
- ");
- $query3 = $this->con->prepare("
- SELECT *
- FROM discussion
- WHERE ArticleID = :id
- ORDER BY ReviewID DESC
- ");
- $query2 ->bindParam(':id', $id);
- $query2->execute();
- if($query2->rowCount() > 0){
- $query3 ->bindParam(':id', $id);
- $query3->execute();
- $fetch2 = array();
- while($forum = $query3->fetchAll(PDO::FETCH_ASSOC)){
- $fetch2[] = $forum;
- }
- }
- else{
- echo "<p>Sorry nothing found</p>";
- }
- $test = json_encode(array($fetch, $fetch2));
- $this->setTest($test);
- //return json_encode(array($fetch, $fetch2));
- }
- public function getuser($obj){
- $this->setTest("");
- $newsub = json_decode($obj);
- $username = $newsub->username;
- $query = $this->con->prepare("
- SELECT *
- FROM user
- WHERE Username = :username OR Email = :username
- ");
- $query ->bindParam(':username', $username);
- $query->execute();
- $fetch = array();
- while($forum = $query->fetchAll(PDO::FETCH_ASSOC)){
- $fetch[] = $forum;
- }
- $test = json_encode($fetch);
- $this->setTest($test);
- //return json_encode($fetch);
- }
- function insertComments($obj){
- $this->setTest("");
- $newsub = json_decode($obj);
- $usernamee = $newsub->username;
- $id = $newsub->id;
- $review = $newsub->review;
- $query2 = $this->con->prepare("SELECT *
- FROM user
- WHERE Username = :startedby OR Email = :startedby");
- $query = $this->con->prepare("INSERT INTO discussion (dismessage, startedby, UserID, ArticleID) VALUES(:dismessage, :startedby, :id, :ArticleID )");
- $query->bindParam(':dismessage',$review);
- $query2->bindParam(':startedby', $usernamee);
- $query->bindParam(':ArticleID', $id);
- $query2->execute();
- $forum = $query2->fetchAll(PDO::FETCH_OBJ);
- foreach ($forum as $forum){
- $id2 = $forum -> UserID;
- $users = $forum -> Username;
- }
- $query->bindParam(':startedby', $users);
- $query->bindParam(':id', $id2);
- $query->execute();
- if($query->rowCount() > 0){
- header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=mainarts&model=mainart&controller=maincon&action=maincon&id=".$id);
- die();
- }
- else{
- echo "";
- }
- }
- function login($objj){
- $this->setTest("");
- $newsub = json_decode($objj);
- $username = $newsub ->username;
- $enc_pass = $newsub->pass;
- $pass = $newsub->password;
- $query = $this->con->prepare("
- SELECT *
- FROM user
- WHERE Username = :username OR Email = :username
- ");
- $query->bindParam(':username', $username);
- //$query ->bindParam(':password', $enc_pass);
- $query->execute();
- $forum = $query->fetchAll(PDO::FETCH_OBJ);
- if($query->rowCount() > 0){
- foreach($forum as $forum){
- $password = $forum->Password;
- }
- $hashver = password_verify($pass,$password);
- if($hashver){
- $_SESSION['Username'] = $username;
- header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=views&model=getArticles");
- die();
- }
- else{
- $output = "Login Information Wrong Please Check Again";
- $test = $output;
- $this->setTest($test);
- }
- }
- else{
- $output = "Login Information Wrong Please Check Again";
- $test = $output;
- $this->setTest($test);
- }
- /* $fetch = array();
- while($forum = $query->fetchAll(PDO::FETCH_ASSOC)){
- $fetch[] = $forum;
- }
- return json_encode($fetch);*/
- }
- public function reg($obj){
- $newsub = json_decode($obj);
- $username = $newsub ->username;
- $enc_pass = $newsub->pass;
- $email = $newsub ->email;
- $Fname = $newsub ->fname;
- $Lname = $newsub->lname;
- $query2 = $this->con->prepare("
- SELECT *
- FROM user
- WHERE username = :username");
- $query2->bindParam(':username', $username);
- $query2->execute();
- $query = $this->con->prepare("INSERT INTO user (Fname, Lname, Email, Username, Password) VALUES(:fname, :lname, :email, :username, :password)");
- $query->bindParam(':fname', $Fname);
- $query->bindParam(':lname', $Lname);
- $query->bindParam(':email', $email);
- $query->bindParam(':username', $username);
- $query->bindParam(':password', $enc_pass);
- if($query2->rowCount() > 0){
- $test = "Username already exists please try diffrent username for registration";
- $this->setTest($test);
- }
- else{
- $query->execute();
- header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=login&model=testing");
- die();
- }
- }
- public function getPages($obj){
- $newsub = json_decode($obj);
- $start = $newsub ->start;
- $perPage = $newsub ->pp;
- $articles = $this->con->prepare("
- SELECT SQL_CALC_FOUND_ROWS *
- FROM Article
- ORDER BY ArticleID DESC
- LIMIT ".$start.",".$perPage."
- ");
- $articles->execute();
- $fetch = array();
- while($forum = $articles->fetchAll(PDO::FETCH_ASSOC)){
- $fetch[] = $forum;
- }
- $total=$this->con->query("SELECT FOUND_ROWS() as total")->fetch()['total'];
- $pages = ceil($total / $perPage);
- return json_encode($pages);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement