Company.php 3.2 KB

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