Guest User

Untitled

a guest
Oct 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Common_model extends CI_Model {
  5.  
  6. public function get_data($array) {
  7. // if set column
  8. if(isset($array['column'])) {
  9. $this->db->select($array['column']);
  10. }
  11. else {
  12. $this->db->select();
  13. }
  14.  
  15. $this->db->from($array['table']);
  16.  
  17. // if set limit and offset
  18. if(isset($array['limit']) && isset($array['offset'])) {
  19. $this->db->limit($array['limit'], $array['offset']);
  20. }
  21.  
  22. // if set where
  23. if(isset($array['where'])) {
  24. $this->db->where($array['where']);
  25. }
  26. // get query
  27. $query = $this->db->get();
  28.  
  29. // if set single
  30. if (isset($array['single'])) {
  31. return $query->row();
  32. }
  33.  
  34. // if set row_array
  35. if(isset($array['row_array'])) {
  36. return $query->row_array();
  37. }
  38.  
  39. // return
  40. return $query->result();
  41. }
  42.  
  43. // Add new data to database
  44. public function add_data($table, $data, $last_id = "") {
  45. $result = $this->db->insert($table, $data);
  46.  
  47. if($last_id) {
  48. $last_id = $this->db->insert_id();
  49. return $multi_id;
  50. }
  51.  
  52. if($result) {
  53. return TRUE;
  54. }
  55. else {
  56. return FALSE;
  57. }
  58. }
  59.  
  60. // Update
  61. public function update_data($table, $data, $where) {
  62. $result = $this->db->where($where)->update($table, $data);
  63. if($result) {
  64. return TRUE;
  65. }
  66. else {
  67. return FALSE;
  68. }
  69. }
  70.  
  71. // Delete
  72. public function delete_data($table, $where) {
  73. $result = $this->db->where($where)->delete($table);
  74. if($result){
  75. return TRUE;
  76. }
  77. else {
  78. return FALSE;
  79. }
  80.  
  81. }
  82.  
  83. // Count numbe of rows of a table
  84. public function num_rows($table, $where=NULL)
  85. {
  86. $this->db
  87. ->select()
  88. ->from($table);
  89. if($where) {
  90. $this->db->where($where);
  91. }
  92. $query = $this->db->get();
  93. return $query->num_rows();
  94. }
  95.  
  96. // joining of tables
  97. public function join_table($table1, $table2, $table1_id, $table2_id, $where=NULL, $single=NULL) {
  98. $this->db->select('*')
  99. ->from($table1)
  100. ->join($table2, "$table1.$table1_id = $table2.$table2_id");
  101. if($where != NULL) {
  102. $this->db->where($where);
  103. }
  104. $query = $this->db->get();
  105. if($single != NULL) {
  106. return $query->row();
  107. }
  108. return $query->result();
  109.  
  110. }
  111.  
  112. public function sum_where($table, $column, $where = NULL) {
  113. $this->db->select_sum($column);
  114. if($where) {
  115. $this->db->where($where);
  116. }
  117. $query = $this->db->get($table)->row();
  118. return $query->$column;
  119. }
  120.  
  121. }
  122.  
  123. /* End of file Common_model.php */
Add Comment
Please, Sign In to add comment