Brand.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. }
  25. else
  26. {
  27. $this->_index();
  28. }
  29. }
  30. //管理
  31. public function _index()
  32. {
  33. $post = $this->input->post(NULL, TRUE);
  34. if(isset($post['page']))
  35. {
  36. $page = $this->input->post('page',true);
  37. $perpage = $this->input->post('perpage',true);
  38. $title = $this->input->post('title',true);
  39. $where = "1=1 ";
  40. //数据排序
  41. $order_str = "id asc";
  42. if(empty($page))
  43. {
  44. $start = 0;
  45. $perpage = 1;
  46. }
  47. else
  48. {
  49. $start = ($page - 1)*$perpage;
  50. }
  51. if($title)
  52. {
  53. $where .= " and title = '$title'";
  54. }
  55. //取得信息列表
  56. $info_list = $this->brand->find_all($where,'id,title,shop',$order_str,$start,$perpage);
  57. //格式化数据
  58. foreach ($info_list as $key=>$value)
  59. {
  60. $t = '';
  61. $shop = explode('|',trim($value['shop'],'|'));
  62. if($shop)
  63. {
  64. foreach ($shop as $v)
  65. {
  66. $s = $this->shop->read($v);
  67. $t .= $s['shopname'].', ';
  68. }
  69. $info_list[$key]['shop'] = rtrim($t,', ');
  70. }
  71. else
  72. {
  73. $info_list[$key]['shop'] = '没有关联店铺';
  74. }
  75. }
  76. $total = $this->brand->find_count($where);
  77. $pagenum = ceil($total/$perpage);
  78. $over = $total-($start+$perpage);
  79. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  80. echo json_encode($rows);exit;
  81. }
  82. $this->_Template('brand',$this->data);
  83. }
  84. //添加
  85. public function _add()
  86. {
  87. $post = $this->input->post(NULL, TRUE);
  88. if(isset($post['title']))
  89. {
  90. $post['title'] = $this->input->post('title',true);
  91. $shop = $this->input->post('shop',true);
  92. if($shop)
  93. {
  94. $shop = trim($shop,',');
  95. $post['shop'] = "|".str_replace(',','|',$shop)."|";
  96. }
  97. if($this->brand->insert($post))
  98. {
  99. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  100. }
  101. else
  102. {
  103. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  104. }
  105. }
  106. $this->_Template('brand_add',$this->data);
  107. }
  108. //修改
  109. public function _edit($arg_array)
  110. {
  111. $post = $this->input->post(NULL, TRUE);
  112. if(isset($post['id']))
  113. {
  114. $id = $this->input->post('id',true);
  115. $post['title'] = $this->input->post('title',true);
  116. $shop = $this->input->post('shop',true);
  117. if($shop)
  118. {
  119. $shop = trim($shop,',');
  120. $post['shop'] = "|".str_replace(',','|',$shop)."|";
  121. }
  122. if($this->brand->save($post,$id))
  123. {
  124. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  125. }
  126. else
  127. {
  128. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  129. }
  130. }
  131. $arg_array = $arg_array[0];
  132. $brand = $this->brand->read($arg_array);
  133. $this->data['brand'] = $brand;
  134. $this->_Template('brand_edit',$this->data);
  135. }
  136. //删除
  137. public function _del()
  138. {
  139. $post = $this->input->post(NULL, TRUE);
  140. if(isset($post['s']))
  141. {
  142. $id_arr = $this->input->post('s');
  143. $id_arr = explode(',',$id_arr);
  144. if(!$id_arr)
  145. {
  146. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  147. }
  148. //循环删除记录
  149. foreach ($id_arr as $v)
  150. {
  151. $this->brand->remove($v);
  152. }
  153. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  154. }
  155. }
  156. }