Source code Website bán hàng online PHP – MVC
81.482 lượt xem;
- MY_Model.php
- core /
- application /
- project /
1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 class MY_Model extends CI_Model {
3
4 // Ten table
5 var $table = '';
6
7 // Key chinh cua table
8 var $key = 'id';
9
10 // Order mac dinh (VD: $order = array('id', 'desc))
11 var $order = '';
12
13 // Cac field select mac dinh khi get_list (VD: $select = 'id, name')
14 var $select = '';
15
16 /**
17 * Them row moi
18 * $data : du lieu ma ta can them
19 */
20 function create($data = array())
21 {
22 if($this->db->insert($this->table, $data))
23 {
24 return TRUE;
25 }else{
26 return FALSE;
27 }
28 }
29
30 /**
31 * Cap nhat row tu id
32 * $id : khoa chinh cua bang can sua
33 * $data : mang du lieu can sua
34 */
35 function update($id, $data)
36 {
37 if (!$id)
38 {
39 return FALSE;
40 }
41
42 $where = array();
43 $where[$this->key] = $id;
44 $this->update_rule($where, $data);
45
46 return TRUE;
47 }
48
49 /**
50 * Cap nhat row tu dieu kien
51 * $where : dieu kien
52 * $data : mang du lieu can cap nhat
53 */
54 function update_rule($where, $data)
55 {
56 if (!$where)
57 {
58 return FALSE;
59 }
60
61 $this->db->where($where);
62 $this->db->update($this->table, $data);
63
64 return TRUE;
65 }
66
67 /**
68 * Xoa row tu id
69 * $id : gia tri cua khoa chinh
70 */
71 function delete($id)
72 {
73 if (!$id)
74 {
75 return FALSE;
76 }
77 //neu la so
78 if(is_numeric($id))
79 {
80 $where = array($this->key => $id);
81 }else
82 {
83 //$id = 1,2,3...
84 $where = $this->key . " IN (".$id.") ";
85 }
86 $this->del_rule($where);
87
88 return TRUE;
89 }
90
91 /**
92 * Xoa row tu dieu kien
93 * $where : mang dieu kien de xoa
94 */
95 function del_rule($where)
96 {
97 if (!$where)
98 {
99 return FALSE;
100 }
101
102 $this->db->where($where);
103 $this->db->delete($this->table);
104
105 return TRUE;
106 }
107
108 /**
109 * Thực hiện câu lệnh query
110 * $sql : cau lenh sql
111 */
112 function query($sql){
113 $rows = $this->db->query($sql);
114 return $rows->result;
115 }
116
117 /**
118 * Lay thong tin cua row tu id
119 * $id : id can lay thong tin
120 * $field : cot du lieu ma can lay
121 */
122 function get_info($id, $field = '')
123 {
124 if (!$id)
125 {
126 return FALSE;
127 }
128
129 $where = array();
130 $where[$this->key] = $id;
131
132 return $this->get_info_rule($where, $field);
133 }
134
135 /**
136 * Lay thong tin cua row tu dieu kien
137 * $where: Mảng điều kiện
138 * $field: Cột muốn lấy dữ liệu
139 */
140 function get_info_rule($where = array(), $field= '')
141 {
142 if($field)
143 {
144 $this->db->select($field);
145 }
146 $this->db->where($where);
147 $query = $this->db->get($this->table);
148 if ($query->num_rows())
149 {
150 return $query->row();
151 }
152
153 return FALSE;
154 }
155
156 /**
157 * Lay tong so
158 */
159 function get_total($input = array())
160 {
161 $this->get_list_set_input($input);
162
163 $query = $this->db->get($this->table);
164
165 return $query->num_rows();
166 }
167
168 /**
169 * Lay tong so
170 * $field: cot muon tinh tong
171 */
172 function get_sum($field, $where = array())
173 {
174 $this->db->select_sum($field);//tinh rong
175 $this->db->where($where);//dieu kien
176 $this->db->from($this->table);
177
178 $row = $this->db->get()->row();
179 foreach ($row as $f => $v)
180 {
181 $sum = $v;
182 }
183 return $sum;
184 }
185
186 /**
187 * Lay 1 row
188 */
189 function get_row($input = array()){
190 $this->get_list_set_input($input);
191
192 $query = $this->db->get($this->table);
193
194 return $query->row();
195 }
196
197 /**
198 * Lay danh sach
199 * $input : mang cac du lieu dau vao
200 */
201 function get_list($input = array())
202 {
203 //xu ly ca du lieu dau vao
204 $this->get_list_set_input($input);
205
206 //thuc hien truy van du lieu
207 $query = $this->db->get($this->table);
208 //echo $this->db->last_query();
209 return $query->result();
210 }
211
212 /**
213 * Gan cac thuoc tinh trong input khi lay danh sach
214 * $input : mang du lieu dau vao
215 */
216
217 protected function get_list_set_input($input = array())
218 {
219
220 // Thêm điều kiện cho câu truy vấn truyền qua biến $input['where']
221 //(vi du: $input['where'] = array('email' => 'hocphp@gmail.com'))
222 if ((isset($input['where'])) && $input['where'])
223 {
224 $this->db->where($input['where']);
225 }
226
227 //tim kiem like
228 // $input['like'] = array('name' , 'abc');
229 if ((isset($input['like'])) && $input['like'])
230 {
231 $this->db->like($input['like'][0], $input['like'][1]);
232 }
233
234 // Thêm sắp xếp dữ liệu thông qua biến $input['order']
235 //(ví dụ $input['order'] = array('id','DESC'))
236 if (isset($input['order'][0]) && isset($input['order'][1]))
237 {
238 $this->db->order_by($input['order'][0], $input['order'][1]);
239 }
240 else
241 {
242 //mặc định sẽ sắp xếp theo id giảm dần
243 $order = ($this->order == '') ? array($this->table.'.'.$this->key, 'desc') : $this->order;
244 $this->db->order_by($order[0], $order[1]);
245 }
246
247 // Thêm điều kiện limit cho câu truy vấn thông qua biến $input['limit']
248 //(ví dụ $input['limit'] = array('10' ,'0'))
249 if (isset($input['limit'][0]) && isset($input['limit'][1]))
250 {
251 $this->db->limit($input['limit'][0], $input['limit'][1]);
252 }
253
254 }
255
256 /**
257 * kiểm tra sự tồn tại của dữ liệu theo 1 điều kiện nào đó
258 * $where : mang du lieu dieu kien
259 */
260 function check_exists($where = array())
261 {
262 $this->db->where($where);
263 //thuc hien cau truy van lay du lieu
264 $query = $this->db->get($this->table);
265
266 if($query->num_rows() > 0){
267 return TRUE;
268 }else{
269 return FALSE;
270 }
271 }
272
273 }
274 ?>
2 class MY_Model extends CI_Model {
3
4 // Ten table
5 var $table = '';
6
7 // Key chinh cua table
8 var $key = 'id';
9
10 // Order mac dinh (VD: $order = array('id', 'desc))
11 var $order = '';
12
13 // Cac field select mac dinh khi get_list (VD: $select = 'id, name')
14 var $select = '';
15
16 /**
17 * Them row moi
18 * $data : du lieu ma ta can them
19 */
20 function create($data = array())
21 {
22 if($this->db->insert($this->table, $data))
23 {
24 return TRUE;
25 }else{
26 return FALSE;
27 }
28 }
29
30 /**
31 * Cap nhat row tu id
32 * $id : khoa chinh cua bang can sua
33 * $data : mang du lieu can sua
34 */
35 function update($id, $data)
36 {
37 if (!$id)
38 {
39 return FALSE;
40 }
41
42 $where = array();
43 $where[$this->key] = $id;
44 $this->update_rule($where, $data);
45
46 return TRUE;
47 }
48
49 /**
50 * Cap nhat row tu dieu kien
51 * $where : dieu kien
52 * $data : mang du lieu can cap nhat
53 */
54 function update_rule($where, $data)
55 {
56 if (!$where)
57 {
58 return FALSE;
59 }
60
61 $this->db->where($where);
62 $this->db->update($this->table, $data);
63
64 return TRUE;
65 }
66
67 /**
68 * Xoa row tu id
69 * $id : gia tri cua khoa chinh
70 */
71 function delete($id)
72 {
73 if (!$id)
74 {
75 return FALSE;
76 }
77 //neu la so
78 if(is_numeric($id))
79 {
80 $where = array($this->key => $id);
81 }else
82 {
83 //$id = 1,2,3...
84 $where = $this->key . " IN (".$id.") ";
85 }
86 $this->del_rule($where);
87
88 return TRUE;
89 }
90
91 /**
92 * Xoa row tu dieu kien
93 * $where : mang dieu kien de xoa
94 */
95 function del_rule($where)
96 {
97 if (!$where)
98 {
99 return FALSE;
100 }
101
102 $this->db->where($where);
103 $this->db->delete($this->table);
104
105 return TRUE;
106 }
107
108 /**
109 * Thực hiện câu lệnh query
110 * $sql : cau lenh sql
111 */
112 function query($sql){
113 $rows = $this->db->query($sql);
114 return $rows->result;
115 }
116
117 /**
118 * Lay thong tin cua row tu id
119 * $id : id can lay thong tin
120 * $field : cot du lieu ma can lay
121 */
122 function get_info($id, $field = '')
123 {
124 if (!$id)
125 {
126 return FALSE;
127 }
128
129 $where = array();
130 $where[$this->key] = $id;
131
132 return $this->get_info_rule($where, $field);
133 }
134
135 /**
136 * Lay thong tin cua row tu dieu kien
137 * $where: Mảng điều kiện
138 * $field: Cột muốn lấy dữ liệu
139 */
140 function get_info_rule($where = array(), $field= '')
141 {
142 if($field)
143 {
144 $this->db->select($field);
145 }
146 $this->db->where($where);
147 $query = $this->db->get($this->table);
148 if ($query->num_rows())
149 {
150 return $query->row();
151 }
152
153 return FALSE;
154 }
155
156 /**
157 * Lay tong so
158 */
159 function get_total($input = array())
160 {
161 $this->get_list_set_input($input);
162
163 $query = $this->db->get($this->table);
164
165 return $query->num_rows();
166 }
167
168 /**
169 * Lay tong so
170 * $field: cot muon tinh tong
171 */
172 function get_sum($field, $where = array())
173 {
174 $this->db->select_sum($field);//tinh rong
175 $this->db->where($where);//dieu kien
176 $this->db->from($this->table);
177
178 $row = $this->db->get()->row();
179 foreach ($row as $f => $v)
180 {
181 $sum = $v;
182 }
183 return $sum;
184 }
185
186 /**
187 * Lay 1 row
188 */
189 function get_row($input = array()){
190 $this->get_list_set_input($input);
191
192 $query = $this->db->get($this->table);
193
194 return $query->row();
195 }
196
197 /**
198 * Lay danh sach
199 * $input : mang cac du lieu dau vao
200 */
201 function get_list($input = array())
202 {
203 //xu ly ca du lieu dau vao
204 $this->get_list_set_input($input);
205
206 //thuc hien truy van du lieu
207 $query = $this->db->get($this->table);
208 //echo $this->db->last_query();
209 return $query->result();
210 }
211
212 /**
213 * Gan cac thuoc tinh trong input khi lay danh sach
214 * $input : mang du lieu dau vao
215 */
216
217 protected function get_list_set_input($input = array())
218 {
219
220 // Thêm điều kiện cho câu truy vấn truyền qua biến $input['where']
221 //(vi du: $input['where'] = array('email' => 'hocphp@gmail.com'))
222 if ((isset($input['where'])) && $input['where'])
223 {
224 $this->db->where($input['where']);
225 }
226
227 //tim kiem like
228 // $input['like'] = array('name' , 'abc');
229 if ((isset($input['like'])) && $input['like'])
230 {
231 $this->db->like($input['like'][0], $input['like'][1]);
232 }
233
234 // Thêm sắp xếp dữ liệu thông qua biến $input['order']
235 //(ví dụ $input['order'] = array('id','DESC'))
236 if (isset($input['order'][0]) && isset($input['order'][1]))
237 {
238 $this->db->order_by($input['order'][0], $input['order'][1]);
239 }
240 else
241 {
242 //mặc định sẽ sắp xếp theo id giảm dần
243 $order = ($this->order == '') ? array($this->table.'.'.$this->key, 'desc') : $this->order;
244 $this->db->order_by($order[0], $order[1]);
245 }
246
247 // Thêm điều kiện limit cho câu truy vấn thông qua biến $input['limit']
248 //(ví dụ $input['limit'] = array('10' ,'0'))
249 if (isset($input['limit'][0]) && isset($input['limit'][1]))
250 {
251 $this->db->limit($input['limit'][0], $input['limit'][1]);
252 }
253
254 }
255
256 /**
257 * kiểm tra sự tồn tại của dữ liệu theo 1 điều kiện nào đó
258 * $where : mang du lieu dieu kien
259 */
260 function check_exists($where = array())
261 {
262 $this->db->where($where);
263 //thuc hien cau truy van lay du lieu
264 $query = $this->db->get($this->table);
265
266 if($query->num_rows() > 0){
267 return TRUE;
268 }else{
269 return FALSE;
270 }
271 }
272
273 }
274 ?>