Advertisement
Guest User

Model

a guest
Dec 5th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. <?php
  2. class Model
  3. {
  4. public $conn;
  5. public $con;
  6. public $testing;
  7. private $test;
  8. public function __construct(){
  9. include("Connect.php");
  10. $this->testing = "";
  11. $this->test = "";
  12. $this->conn = new con;
  13. $this->con = $this->conn->connect();
  14. }
  15. public function setTest($test){
  16. $this->test = $test;
  17. }
  18. public function getTest(){
  19. return $this->test;
  20. }
  21. public function testing(){
  22. $output= $this->testing = "hi there friend :)";
  23. return $output;
  24. }
  25.  
  26. public function getArticles($obj){
  27. $this->setTest("");
  28. $newsub = json_decode($obj);
  29. $start = $newsub ->start;
  30. $perPage = $newsub ->pp;
  31. $articles = $this->con->prepare("
  32. SELECT SQL_CALC_FOUND_ROWS *
  33. FROM Article
  34. ORDER BY ArticleID DESC
  35. LIMIT ".$start.",".$perPage."
  36. ");
  37.  
  38. $articles->execute();
  39.  
  40. $fetch = array();
  41. while($forum = $articles->fetchAll(PDO::FETCH_ASSOC)){
  42. $fetch[] = $forum;
  43. }
  44. $test = json_encode($fetch);
  45. $this->setTest($test);
  46. //return json_encode($fetch);
  47. }
  48.  
  49. public function mainart($obj){
  50. $this->setTest("");
  51. $newsub = json_decode($obj);
  52. $id = $newsub->id;
  53. $query = $this->con->prepare("
  54. SELECT *
  55. FROM Article, Main
  56. WHERE Main.Articleid = Article.ArticleID AND Main.ArticleID = :id
  57. ");
  58. $query ->bindParam(':id', $id);
  59. $query->execute();
  60. $fetch = array();
  61. while($forum = $query->fetchAll(PDO::FETCH_ASSOC)){
  62. $fetch[] = $forum;
  63. }
  64.  
  65. $query2 = $this->con->prepare("
  66. SELECT *
  67. FROM Article
  68. WHERE ArticleID = :id
  69. ");
  70.  
  71. $query3 = $this->con->prepare("
  72. SELECT *
  73. FROM discussion
  74. WHERE ArticleID = :id
  75. ORDER BY ReviewID DESC
  76. ");
  77. $query2 ->bindParam(':id', $id);
  78. $query2->execute();
  79. if($query2->rowCount() > 0){
  80. $query3 ->bindParam(':id', $id);
  81. $query3->execute();
  82.  
  83. $fetch2 = array();
  84. while($forum = $query3->fetchAll(PDO::FETCH_ASSOC)){
  85. $fetch2[] = $forum;
  86. }
  87. }
  88.  
  89. else{
  90. echo "<p>Sorry nothing found</p>";
  91. }
  92. $test = json_encode(array($fetch, $fetch2));
  93. $this->setTest($test);
  94.  
  95. //return json_encode(array($fetch, $fetch2));
  96. }
  97.  
  98.  
  99.  
  100. public function getuser($obj){
  101. $this->setTest("");
  102. $newsub = json_decode($obj);
  103. $username = $newsub->username;
  104. $query = $this->con->prepare("
  105. SELECT *
  106. FROM user
  107. WHERE Username = :username OR Email = :username
  108.  
  109.  
  110. ");
  111. $query ->bindParam(':username', $username);
  112. $query->execute();
  113. $fetch = array();
  114. while($forum = $query->fetchAll(PDO::FETCH_ASSOC)){
  115. $fetch[] = $forum;
  116. }
  117. $test = json_encode($fetch);
  118. $this->setTest($test);
  119. //return json_encode($fetch);
  120. }
  121.  
  122.  
  123.  
  124.  
  125. function insertComments($obj){
  126. $this->setTest("");
  127. $newsub = json_decode($obj);
  128.  
  129. $usernamee = $newsub->username;
  130. $id = $newsub->id;
  131. $review = $newsub->review;
  132.  
  133.  
  134. $query2 = $this->con->prepare("SELECT *
  135. FROM user
  136. WHERE Username = :startedby OR Email = :startedby");
  137. $query = $this->con->prepare("INSERT INTO discussion (dismessage, startedby, UserID, ArticleID) VALUES(:dismessage, :startedby, :id, :ArticleID )");
  138. $query->bindParam(':dismessage',$review);
  139.  
  140.  
  141. $query2->bindParam(':startedby', $usernamee);
  142. $query->bindParam(':ArticleID', $id);
  143. $query2->execute();
  144. $forum = $query2->fetchAll(PDO::FETCH_OBJ);
  145. foreach ($forum as $forum){
  146. $id2 = $forum -> UserID;
  147. $users = $forum -> Username;
  148. }
  149. $query->bindParam(':startedby', $users);
  150. $query->bindParam(':id', $id2);
  151. $query->execute();
  152.  
  153.  
  154.  
  155. if($query->rowCount() > 0){
  156. header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=mainarts&model=mainart&controller=maincon&action=maincon&id=".$id);
  157. die();
  158.  
  159.  
  160. }
  161. else{
  162. echo "";
  163.  
  164. }
  165.  
  166. }
  167.  
  168.  
  169.  
  170. function login($objj){
  171. $this->setTest("");
  172. $newsub = json_decode($objj);
  173. $username = $newsub ->username;
  174. $enc_pass = $newsub->pass;
  175. $pass = $newsub->password;
  176. $query = $this->con->prepare("
  177. SELECT *
  178. FROM user
  179. WHERE Username = :username OR Email = :username
  180. ");
  181. $query->bindParam(':username', $username);
  182.  
  183. //$query ->bindParam(':password', $enc_pass);
  184. $query->execute();
  185. $forum = $query->fetchAll(PDO::FETCH_OBJ);
  186. if($query->rowCount() > 0){
  187. foreach($forum as $forum){
  188. $password = $forum->Password;
  189. }
  190. $hashver = password_verify($pass,$password);
  191. if($hashver){
  192. $_SESSION['Username'] = $username;
  193. header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=views&model=getArticles");
  194. die();
  195. }
  196. else{
  197. $output = "Login Information Wrong Please Check Again";
  198. $test = $output;
  199. $this->setTest($test);
  200. }
  201.  
  202. }
  203. else{
  204. $output = "Login Information Wrong Please Check Again";
  205. $test = $output;
  206. $this->setTest($test);
  207. }
  208. /* $fetch = array();
  209. while($forum = $query->fetchAll(PDO::FETCH_ASSOC)){
  210. $fetch[] = $forum;
  211. }
  212. return json_encode($fetch);*/
  213.  
  214.  
  215. }
  216.  
  217. public function reg($obj){
  218. $newsub = json_decode($obj);
  219. $username = $newsub ->username;
  220. $enc_pass = $newsub->pass;
  221. $email = $newsub ->email;
  222. $Fname = $newsub ->fname;
  223. $Lname = $newsub->lname;
  224.  
  225. $query2 = $this->con->prepare("
  226. SELECT *
  227. FROM user
  228. WHERE username = :username");
  229. $query2->bindParam(':username', $username);
  230. $query2->execute();
  231. $query = $this->con->prepare("INSERT INTO user (Fname, Lname, Email, Username, Password) VALUES(:fname, :lname, :email, :username, :password)");
  232. $query->bindParam(':fname', $Fname);
  233. $query->bindParam(':lname', $Lname);
  234. $query->bindParam(':email', $email);
  235. $query->bindParam(':username', $username);
  236. $query->bindParam(':password', $enc_pass);
  237.  
  238.  
  239. if($query2->rowCount() > 0){
  240. $test = "Username already exists please try diffrent username for registration";
  241. $this->setTest($test);
  242.  
  243.  
  244. }
  245. else{
  246. $query->execute();
  247. header("Location: https://mayar.abertay.ac.uk/~1800854/MVC/index.php?page=login&model=testing");
  248. die();
  249. }
  250.  
  251. }
  252.  
  253. public function getPages($obj){
  254. $newsub = json_decode($obj);
  255. $start = $newsub ->start;
  256. $perPage = $newsub ->pp;
  257. $articles = $this->con->prepare("
  258. SELECT SQL_CALC_FOUND_ROWS *
  259. FROM Article
  260. ORDER BY ArticleID DESC
  261. LIMIT ".$start.",".$perPage."
  262.  
  263. ");
  264.  
  265. $articles->execute();
  266.  
  267. $fetch = array();
  268. while($forum = $articles->fetchAll(PDO::FETCH_ASSOC)){
  269. $fetch[] = $forum;
  270. }
  271.  
  272. $total=$this->con->query("SELECT FOUND_ROWS() as total")->fetch()['total'];
  273.  
  274. $pages = ceil($total / $perPage);
  275.  
  276. return json_encode($pages);
  277. }
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement