Shopsku.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Shopsku extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->_model('Model_shopsku','shopsku');
  6. $this->load->_model('Model_typeclass','typeclass');
  7. $this->load->_model('Model_shop','shop');
  8. }
  9. //定义方法的调用规则 获取URI第二段值
  10. public function _remap($arg,$arg_array)
  11. {
  12. if($arg == 'add')
  13. {
  14. $this->_add();
  15. }
  16. else if($arg == 'edit')
  17. {
  18. $this->_edit($arg_array);
  19. }
  20. else if($arg == 'del')
  21. {
  22. $this->_del();
  23. }
  24. else
  25. {
  26. $this->_index();
  27. }
  28. }
  29. public function _index()
  30. {
  31. $post = $this->input->post(NULL, TRUE);
  32. if(isset($post['page']))
  33. {
  34. $page = $this->input->post('page',true);
  35. $perpage = $this->input->post('perpage',true);
  36. $title = $this->input->post('title',true);
  37. $where = "1=1";
  38. if($title)
  39. {
  40. $where .= " and title like '%$title%'";
  41. }
  42. //数据排序
  43. $order_str = "id desc";
  44. if(empty($page))
  45. {
  46. $start = 0;
  47. $perpage = 1;
  48. }
  49. else
  50. {
  51. $start = ($page - 1)*$perpage;
  52. }
  53. $typeclass = array();
  54. $class = $this->typeclass->find_all();
  55. foreach ($class as $v)
  56. {
  57. $v['spare'] = explode('|',$v['spare']);
  58. $typeclass[$v['id']] = $v['spare'][0];
  59. }
  60. $s = array();
  61. $shop = $this->shop->find_all();
  62. foreach ($shop as $v)
  63. {
  64. $s[$v['id']] = $v['shopname'];
  65. }
  66. $info_list = $this->shopsku->find_all($where,'id,shop,sku',$order_str,$start,$perpage);
  67. foreach ($info_list as $key=>$value)
  68. {
  69. $info_list[$key]['sku'] = '';
  70. $info_list[$key]['shop'] = '';
  71. $sku = explode(',',trim($value['sku'],','));
  72. foreach ($sku as $v)
  73. {
  74. $info_list[$key]['sku'] .= $v.'<br>';
  75. }
  76. $shop = explode(',',trim($value['shop'],','));
  77. foreach ($shop as $v)
  78. {
  79. $v = $s[$v];
  80. $info_list[$key]['shop'] .= $v.'<br>';
  81. }
  82. }
  83. $total = $this->shopsku->find_count($where);
  84. $pagenum = ceil($total/$perpage);
  85. $over = $total-($start+$perpage);
  86. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  87. echo json_encode($rows);exit;
  88. }
  89. $this->_Template('shopsku',$this->data);
  90. }
  91. public function _add()
  92. {
  93. $post = $this->input->post(NULL, TRUE);
  94. if(isset($post['sku']))
  95. {
  96. if($this->shopsku->insert($post))
  97. {
  98. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  99. }
  100. else
  101. {
  102. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  103. }
  104. }
  105. $this->_Template('shopsku_add',$this->data);
  106. }
  107. public function _edit($arg_array)
  108. {
  109. $post = $this->input->post(NULL, TRUE);
  110. if(isset($post['id']))
  111. {
  112. $id = $this->input->post('id',true);
  113. if($this->shopsku->save($post,$id))
  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. $arg_array = $arg_array[0];
  123. $shopsku = $this->shopsku->read($arg_array);
  124. $this->data['shopsku'] = $shopsku;
  125. $this->_Template('shopsku_edit',$this->data);
  126. }
  127. //删除
  128. public function _del()
  129. {
  130. $post = $this->input->post(NULL, TRUE);
  131. if(isset($post['s']))
  132. {
  133. $id_arr = $this->input->post('s');
  134. $id_arr = explode(',',$id_arr);
  135. if(!$id_arr)
  136. {
  137. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  138. }
  139. //循环删除记录
  140. foreach ($id_arr as $v)
  141. {
  142. $this->shopsku->remove($v);
  143. }
  144. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  145. }
  146. }
  147. }