Country.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Country extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_country','country');
  7. $this->load->_model('Model_typeclass','typeclass');
  8. $this->load->_model('Model_express','express');
  9. $this->load->_model('Model_excel','excel');
  10. }
  11. //定义方法的调用规则 获取URI第二段值
  12. public function _remap($arg,$arg_array)
  13. {
  14. if($arg == 'add')//添加
  15. {
  16. $this->_add();
  17. }
  18. else if($arg == 'edit')//修改
  19. {
  20. $this->_edit($arg_array);
  21. }
  22. else if($arg == 'del')//修改
  23. {
  24. $this->_del();
  25. }
  26. else if($arg == 'rows')//获取数据
  27. {
  28. $this->_rows();
  29. }
  30. else if($arg == 'excel')//获取数据
  31. {
  32. $this->_excel();
  33. }
  34. else
  35. {
  36. $this->_index();
  37. }
  38. }
  39. //管理
  40. public function _index()
  41. {
  42. $post = $this->input->post(NULL, TRUE);
  43. if(isset($post['page']))
  44. {
  45. $page = $this->input->post('page',true);
  46. $perpage = $this->input->post('perpage',true);
  47. $continent = $this->input->post('continent',true);
  48. $express = $this->input->post('express',true);
  49. $ename = $this->input->post('ename',true);
  50. $zname = $this->input->post('zname',true);
  51. $where = "1=1 ";
  52. if($continent)
  53. {
  54. $where .= " and continent = '$continent'";
  55. }
  56. if($express)
  57. {
  58. $where .= " and express = '$express'";
  59. }
  60. if($ename)
  61. {
  62. $where .= " and ename like '%$ename%'";
  63. }
  64. if($zname)
  65. {
  66. $where .= " and zname like '%$zname%'";
  67. }
  68. //数据排序
  69. $order_str = "id asc";
  70. if(empty($page))
  71. {
  72. $start = 0;
  73. $perpage = 1;
  74. }
  75. else
  76. {
  77. $start = ($page - 1)*$perpage;
  78. }
  79. //取得信息列表
  80. $info_list = $this->country->find_all($where,'id,name,ename,zname,continent,al,lb,threebitcode,express',$order_str,$start,$perpage);
  81. foreach ($info_list as $key=>$value)
  82. {
  83. if($value['express'] != 0)
  84. {
  85. $express = $this->express->read($value['express']);
  86. $info_list[$key]['express'] = $express['servicename'];
  87. }
  88. else
  89. {
  90. $info_list[$key]['express'] = "";
  91. }
  92. $class = $this->typeclass->read($value['continent']);
  93. $info_list[$key]['continent'] = $class['title'];
  94. }
  95. $total = $this->country->find_count($where);
  96. $pagenum = ceil($total/$perpage);
  97. $over = $total-($start+$perpage);
  98. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  99. echo json_encode($rows);exit;
  100. }
  101. $class = $this->typeclass->find_all('classid = 2','id,title');
  102. $this->data['class'] = $class;
  103. $express = $this->express->find_all();
  104. $this->data['express'] = $express;
  105. $this->_Template('country',$this->data);
  106. }
  107. //添加
  108. public function _add()
  109. {
  110. $post = $this->input->post(NULL, TRUE);
  111. if(isset($post['zname']))
  112. {
  113. if($this->country->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. $class = $this->typeclass->find_all('classid = 2','id,title');
  123. $this->data['class'] = $class;
  124. $express = $this->express->find_all();
  125. $this->data['express'] = $express;
  126. $this->_Template('country_add',$this->data);
  127. }
  128. //修改
  129. public function _edit($arg_array)
  130. {
  131. $post = $this->input->post(NULL, TRUE);
  132. if(isset($post['id']))
  133. {
  134. $id = $this->input->post('id',true);
  135. if($this->country->save($post,$id))
  136. {
  137. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  138. }
  139. else
  140. {
  141. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  142. }
  143. }
  144. $arg_array = $arg_array[0];
  145. $country = $this->country->read($arg_array);
  146. $class = $this->typeclass->find_all('classid = 2','id,title');
  147. $this->data['class'] = $class;
  148. $this->data['country'] = $country;
  149. $express = $this->express->find_all();
  150. $this->data['express'] = $express;
  151. $this->_Template('country_edit',$this->data);
  152. }
  153. //删除
  154. public function _del()
  155. {
  156. $post = $this->input->post(NULL, TRUE);
  157. if(isset($post['s']))
  158. {
  159. $id_arr = $this->input->post('s');
  160. $id_arr = explode(',',$id_arr);
  161. if(!$id_arr)
  162. {
  163. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  164. }
  165. //循环删除记录
  166. foreach ($id_arr as $v)
  167. {
  168. $this->country->remove($v);
  169. }
  170. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  171. }
  172. }
  173. //删除
  174. public function _rows()
  175. {
  176. $this->load->library('pagination');
  177. $config['base_url'] = 'http://127.0.0.1/country/rows';
  178. $config['total_rows'] = 200;
  179. $config['per_page'] = 20;
  180. $this->pagination->initialize($config);
  181. echo $this->pagination->create_links();
  182. }
  183. public function _excel()
  184. {
  185. $post = $this->input->get(NULL, TRUE);
  186. if(isset($post['excel']))
  187. {
  188. $express = $this->express->find_all();
  189. $ex = array();
  190. foreach ($express as $v)
  191. {
  192. $ex[$v['id']] = $v['servicename'];
  193. }
  194. $info_list = $this->country->find_all('1=1','name,zname,ename,threebitcode,al,lb,express','name asc');
  195. foreach ($info_list as $key=>$value)
  196. {
  197. if($value['express'] > 0)
  198. {
  199. $info_list[$key]['express'] = $ex[$value['express']];
  200. }
  201. else
  202. {
  203. $info_list[$key]['express'] = '';
  204. }
  205. }
  206. $title = "国家信息";
  207. $titlename = "<table border=1>
  208. <tr><th colspan='6' align='left'><h3>".$title."<h3></th></tr>
  209. <tr>
  210. <td>国家名称</td>
  211. <td>中文名称</td>
  212. <td>英文名称</td>
  213. <td>三字码</td>
  214. <td>阿里编码</td>
  215. <td>联邦编码</td>
  216. <td>默认物流</td>
  217. </tr>
  218. </table>";
  219. $filename = $title.".xls";
  220. $tail = "\n";
  221. $this->excel->get_fz2($info_list,$titlename,$filename,$tail);
  222. }
  223. }
  224. }