news_model.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. class News_model extends MY_Model
  3. {
  4. private $table_name = 'articles';
  5. private $orderby = 'publishtime';
  6. public function insert($parm)
  7. {
  8. $this->db->insert($this->table_name, $parm);
  9. return $this->db->insert_id();
  10. }
  11. public function delete($id) {
  12. $this->db->where('id', $id);
  13. $this->db->delete($this->table_name);
  14. return $this->db->affected_rows();
  15. }
  16. public function update($id, $parm) {
  17. $this->db->where('id', $id);
  18. $this->db->update($this->table_name, $parm);
  19. return $this->db->affected_rows();
  20. }
  21. public function get($id)
  22. {
  23. $this->db->select('*');
  24. $this->db->from($this->table_name);
  25. $this->db->where('id', $id);
  26. $query = $this->db->get();
  27. return $query->num_rows() > 0 ? $query->row_array() : false;
  28. }
  29. public function all($type)
  30. {
  31. $this->db->where('type', $type);
  32. $this->db->order_by($this->orderby, 'desc');
  33. $query = $this->db->get($this->table_name);
  34. return $query->num_rows() > 0 ? $query->result_array() : false;
  35. }
  36. public function max_ord($type)
  37. {
  38. $this->db->where('type', $type);
  39. $this->db->select('max(ord) as max_ord');
  40. $query = $this->db->get($this->table_name);
  41. $ret = $query->row_array();
  42. return $ret['max_ord'];
  43. }
  44. public function get_last($type,$offset=0, $rows=5)
  45. {
  46. $this->db->where('type', $type);
  47. $this->db->order_by('publishtime', 'desc');
  48. $this->db->limit($rows, $offset);
  49. $query = $this->db->get($this->table_name);
  50. return $query->num_rows() > 0 ? $query->result_array() : false;
  51. }
  52. public function count()
  53. {
  54. $this->db->select('count(*) as count');
  55. $query = $this->db->get($this->table_name);
  56. $ret = $query->row_array();
  57. return $ret['count'];
  58. }
  59. public function all_order_time()
  60. {
  61. $this->db->order_by('publishtime', 'desc');
  62. $query = $this->db->get($this->table_name);
  63. return $query->num_rows() > 0 ? $query->result_array() : false;
  64. }
  65. }