model_main.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. class Model_main extends Model
  3. {
  4. public function get_posts(){
  5. $q = "SELECT * FROM `posts` ORDER BY `updated_at` DESC";
  6. return $this->db->query($q);
  7. }
  8. public function get_post($id){
  9. $q = "SELECT * FROM `posts` WHERE `id` = " . $this->db->escape($id);
  10. $ret = $this->db->query($q);
  11. if(!empty($ret[0])) return $ret[0];
  12. return false;
  13. }
  14. public function create_post($caption, $post){
  15. $q = "INSERT INTO `posts` SET `caption` = " . $this->db->escape($caption) .
  16. ", `post` = " . $this->db->escape($post) .
  17. ", `user_id` = " . $this->db->escape($_SESSION['user']['id']);
  18. $this->db->query($q);
  19. }
  20. public function get_comments($post_id){
  21. $q = "SELECT * FROM `comments` WHERE `post_id` = " . $this->db->escape($post_id) . " ORDER BY `created_at` DESC";
  22. return $this->db->query($q);
  23. }
  24. public function add_comment($post_id, $user_id, $comment){
  25. $q = "INSERT INTO `comments` SET ".
  26. " `post_id` = " . $this->db->escape($post_id) .
  27. ", `user_id` = " . $this->db->escape($user_id) .
  28. ", `comment` = " . $this->db->escape($comment);
  29. $this->db->query($q);
  30. }
  31. }