Advertisement
Venciity

QuestionsModel

May 9th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.32 KB | None | 0 0
  1. <?php
  2.  
  3. class QuestionsModel extends BaseModel{
  4.     public function getAll(){
  5.         $statement = self::$db->query(
  6.             "SELECT q.id, q.text, q.content, q.visits_count, c.text as category, u.username as user
  7.             FROM questions as q LIMIT ?, ?
  8.               JOIN categories as c ON q.category_id = c.id
  9.               JOIN users as u ON q.user_id = u.id ORDER DESC BY id");
  10.         return $statement->fetch_all(MYSQLI_ASSOC);
  11.     }
  12.  
  13.     public function getFilteredQuestions($from, $size){
  14.         $statement = self::$db->prepare("SELECT q.id, q.text, q.content, q.visits_count, c.text as category, u.username as user
  15.             FROM questions as q
  16.               JOIN categories as c ON q.category_id = c.id
  17.               JOIN users as u ON q.user_id = u.id ORDER BY id DESC LIMIT ?, ?");
  18.         $statement->bind_param("ii", $from, $size);
  19.         $statement->execute();
  20.         $result = $statement->get_result()->fetch_all();
  21.         return $result;
  22.     }
  23.  
  24.     public function getQuestionInfo($id){
  25.         $statement = self::$db->prepare("UPDATE questions SET visits_count = visits_count + 1 WHERE id = ?");
  26.         $statement->bind_param("i", $id);
  27.         $statement->execute();
  28.  
  29.         $statement = self::$db->query(
  30.             "SELECT q.id, q.text, q.content, q.visits_count, c.text as category, u.username as user
  31.             FROM questions as q
  32.               JOIN categories as c ON q.category_id = c.id
  33.               JOIN users as u ON q.user_id = u.id WHERE q.id = $id ORDER BY q.id");
  34.         return $statement->fetch_all(MYSQLI_ASSOC);
  35.     }
  36.  
  37.     public function getQuestionTagsByQuestionId($id){
  38.         $statement = self::$db->query("SELECT t.text
  39.             FROM questions as q
  40.               JOIN categories as c ON q.category_id = c.id
  41.               JOIN questions_tags qt ON q.id = qt.question_id
  42.               JOIN tags t ON t.id = qt.tag_id
  43.               WHERE q.id = $id");
  44.         return $statement->fetch_all(MYSQLI_ASSOC);
  45.     }
  46.  
  47.     public function getCategoryIdByText($text){
  48.         $statement = self::$db->prepare("SELECT id FROM categories where text = ?");
  49.         $statement->bind_param("s", $text);
  50.         $statement->execute();
  51.         $result = $statement->get_result()->fetch_assoc();
  52.         return $result['id'];
  53.     }
  54.  
  55.     public function getCurrentUserId(){
  56.         $username = $_SESSION['username'];
  57.         $statement = self::$db->prepare("SELECT id FROM users where username = ?");
  58.         $statement->bind_param("s", $username);
  59.         $statement->execute();
  60.         $result = $statement->get_result()->fetch_assoc();
  61.         return $result['id'];
  62.     }
  63.  
  64.     public function getAllComments($id){
  65.         $statement = self::$db->query(
  66.             "SELECT c.id, c.text, u.username as user
  67.             FROM questions as q
  68.               JOIN comments as c on q.id = c.question_id
  69.               JOIN users as u on u.id = c.user_id
  70.               where q.id = $id");
  71.         return $statement->fetch_all(MYSQLI_ASSOC);
  72.     }
  73.  
  74.     public function createQuestion($text, $content, $userId, $categoryId, $tags) {
  75.         if ($text == '') {
  76.             return false;
  77.         }
  78.  
  79.         $statement = self::$db->prepare("INSERT INTO questions(id, text, content, user_id, category_id) VALUES(NULL, ?, ?, ?, ?)");
  80.         $statement->bind_param("ssii", $text, $content, $userId, $categoryId);
  81.         $statement->execute();
  82.  
  83.         $statement2 = self::$db->prepare("INSERT INTO questions_tags(question_id, tag_id) VALUES(?, ?)");
  84.         $statement2->bind_param("ii", $question_id, $tag_id);
  85.         $statement2->execute();
  86.         return $statement->affected_rows > 0;
  87.     }
  88.  
  89.     // TODO: USE IT ************************************************************************************************************
  90.     public function getTagIdByText($text){
  91.         $statement = self::$db->prepare("SELECT id FROM forum.tags where text = ? ");
  92.         $statement->bind_param("s", $text);
  93.         $statement->execute();
  94.         $result = $statement->get_result()->fetch_assoc();
  95.         return $result['id'];
  96.     }
  97.  
  98.     public function deleteQuestion($id) {
  99.         $statement = self::$db->prepare("DELETE FROM questions WHERE id = ?");
  100.         $statement->bind_param("i", $id);
  101.         $statement->execute();
  102.         return $statement->affected_rows > 0;
  103.     }
  104. }
  105. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement