Brand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Brand extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_user','user');
  7. $this->load->_model('Model_shop','shop');
  8. $this->load->_model('Model_brand','brand');
  9. }
  10. //定义方法的调用规则 获取URI第二段值
  11. public function _remap($arg,$arg_array)
  12. {
  13. if($arg == 'add')//添加
  14. {
  15. $this->_add();
  16. }
  17. else if($arg == 'edit')//修改
  18. {
  19. $this->_edit($arg_array);
  20. }
  21. else if($arg == 'del')//修改
  22. {
  23. $this->_del();
  24. }else if($arg == 'adddo'){
  25. $this->_adddo();
  26. }
  27. else
  28. {
  29. $this->_index();
  30. }
  31. }
  32. private function _adddo(){
  33. $data = $this->input->post(NULL, TRUE);
  34. $params = json_decode($data['data'],true);
  35. $final_list = [];
  36. foreach ($params as $key => $value) {
  37. $final_list[] = [
  38. 'one'=>$value[0],
  39. 'two'=>$value[1],
  40. 'three'=>$value[2],
  41. 'four'=>$value[3],
  42. ];
  43. }
  44. $this->db->insert_batch('zzlvtmp', $final_list);
  45. }
  46. //管理
  47. public function _index()
  48. {
  49. $post = $this->input->post(NULL, TRUE);
  50. if(isset($post['page']))
  51. {
  52. $page = $this->input->post('page',true);
  53. $perpage = $this->input->post('perpage',true);
  54. $title = $this->input->post('title',true);
  55. $where = "1=1 ";
  56. //数据排序
  57. $order_str = "id asc";
  58. if(empty($page))
  59. {
  60. $start = 0;
  61. $perpage = 1;
  62. }
  63. else
  64. {
  65. $start = ($page - 1)*$perpage;
  66. }
  67. if($title)
  68. {
  69. $where .= " and title = '$title'";
  70. }
  71. //取得信息列表
  72. $info_list = $this->brand->find_all($where,'id,title,shop',$order_str,$start,$perpage);
  73. //格式化数据
  74. foreach ($info_list as $key=>$value)
  75. {
  76. $t = '';
  77. $shop = explode('|',trim($value['shop'],'|'));
  78. if($shop)
  79. {
  80. foreach ($shop as $v)
  81. {
  82. $s = $this->shop->read($v);
  83. $t .= $s['shopname'].', ';
  84. }
  85. $info_list[$key]['shop'] = rtrim($t,', ');
  86. }
  87. else
  88. {
  89. $info_list[$key]['shop'] = '没有关联店铺';
  90. }
  91. }
  92. $total = $this->brand->find_count($where);
  93. $pagenum = ceil($total/$perpage);
  94. $over = $total-($start+$perpage);
  95. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  96. echo json_encode($rows);exit;
  97. }
  98. $this->_Template('brand',$this->data);
  99. }
  100. //添加
  101. public function _add()
  102. {
  103. $post = $this->input->post(NULL, TRUE);
  104. if(isset($post['title']))
  105. {
  106. $post['title'] = $this->input->post('title',true);
  107. $shop = $this->input->post('shop',true);
  108. if($shop)
  109. {
  110. $shop = trim($shop,',');
  111. $post['shop'] = "|".str_replace(',','|',$shop)."|";
  112. }
  113. if($this->brand->insert($post))
  114. {
  115. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  116. }
  117. else
  118. {
  119. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  120. }
  121. }
  122. $this->_Template('brand_add',$this->data);
  123. }
  124. //修改
  125. public function _edit($arg_array)
  126. {
  127. $post = $this->input->post(NULL, TRUE);
  128. if(isset($post['id']))
  129. {
  130. $id = $this->input->post('id',true);
  131. $post['title'] = $this->input->post('title',true);
  132. $shop = $this->input->post('shop',true);
  133. if($shop)
  134. {
  135. $shop = trim($shop,',');
  136. $post['shop'] = "|".str_replace(',','|',$shop)."|";
  137. }
  138. if($this->brand->save($post,$id))
  139. {
  140. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  141. }
  142. else
  143. {
  144. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  145. }
  146. }
  147. $arg_array = $arg_array[0];
  148. $brand = $this->brand->read($arg_array);
  149. $this->data['brand'] = $brand;
  150. $this->_Template('brand_edit',$this->data);
  151. }
  152. //删除
  153. public function _del()
  154. {
  155. $post = $this->input->post(NULL, TRUE);
  156. if(isset($post['s']))
  157. {
  158. $id_arr = $this->input->post('s');
  159. $id_arr = explode(',',$id_arr);
  160. if(!$id_arr)
  161. {
  162. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  163. }
  164. //循环删除记录
  165. foreach ($id_arr as $v)
  166. {
  167. $this->brand->remove($v);
  168. }
  169. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  170. }
  171. }
  172. }