Study.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class Study extends Start_Controller {
  3. public function __construct(){
  4. parent::__construct();
  5. $this->load->library('session');
  6. $this->load->_model('Model_study','study');
  7. $this->load->_model('Model_studyclass','studyclass');
  8. $this->load->_model('Model_typeclass','typeclass');
  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 == 'see')
  22. {
  23. $this->_see($arg_array);
  24. }
  25. else if($arg == 'del')
  26. {
  27. $this->_del($arg_array);
  28. }
  29. else
  30. {
  31. $this->_index($arg_array);
  32. }
  33. }
  34. //管理
  35. public function _index($arg_array)
  36. {
  37. $post = $this->input->post(NULL, TRUE);
  38. if(isset($post['page']))
  39. {
  40. $page = $this->input->post('page',true);
  41. $perpage = $this->input->post('perpage',true);
  42. $title = $this->input->post('title',true);
  43. $count = $this->input->post('count',true);
  44. $type = $this->input->post('type',true);
  45. $where = "1=1 ";
  46. //数据排序
  47. $order_str = "id asc";
  48. if(empty($page))
  49. {
  50. $start = 0;
  51. $perpage = 1;
  52. }
  53. else
  54. {
  55. $start = ($page - 1)*$perpage;
  56. }
  57. if($title)
  58. {
  59. $where .= " and title like '%$title%'";
  60. }
  61. if($count)
  62. {
  63. $where .= " and count like '%$count%'";
  64. }
  65. if($type)
  66. {
  67. $where .= " and type = '$type'";
  68. }
  69. //取得信息列表
  70. $info_list = $this->study->find_all($where,'id,type,class,title,addtime',$order_str,$start,$perpage);
  71. //格式化数据
  72. foreach ($info_list as $key=>$value)
  73. {
  74. $type = $this->typeclass->read($value['type']);
  75. $info_list[$key]['type'] = $type['title'];
  76. $f = $this->studyclass->read($value['class']);
  77. $info_list[$key]['class'] = $f['title'];
  78. $d = $this->study->read($value['id']);
  79. if($d['edittime'] != 0)
  80. {
  81. $info_list[$key]['addtime'] = date("Y-m-d H:i:s",$d['edittime']);
  82. }
  83. else
  84. {
  85. $info_list[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
  86. }
  87. }
  88. $total = $this->study->find_count($where);
  89. $pagenum = ceil($total/$perpage);
  90. $over = $total-($start+$perpage);
  91. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  92. echo json_encode($rows);exit;
  93. }
  94. $studyclass = $this->studyclass->find_all();
  95. $this->data['see'] = (isset($arg_array[0]))?'see':'';
  96. $this->data['studyclass'] = $studyclass;
  97. $this->_Template('study',$this->data);
  98. }
  99. //添加
  100. public function _add()
  101. {
  102. $post = $this->input->post(NULL, TRUE);
  103. if(isset($post['title']))
  104. {
  105. $post['addtime'] = time();
  106. $f = $this->studyclass->read($post['class']);
  107. $post['type'] = $f['type'];
  108. if($this->study->insert($post))
  109. {
  110. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  111. }
  112. else
  113. {
  114. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  115. }
  116. }
  117. $studyclass = $this->studyclass->find_all();
  118. $this->data['studyclass'] = $studyclass;
  119. $this->_Template('study_add',$this->data);
  120. }
  121. //修改
  122. public function _edit($arg_array)
  123. {
  124. $post = $this->input->post(NULL, TRUE);
  125. if(isset($post['id']))
  126. {
  127. $id = $this->input->post('id',true);
  128. $post['title'] = $this->input->post('title',true);
  129. $post['edittime'] = time();
  130. $f = $this->studyclass->read($post['class']);
  131. $post['type'] = $f['type'];
  132. if($this->study->save($post,$id))
  133. {
  134. echo json_encode(array('msg'=>'修改成功','success'=>true));exit;
  135. }
  136. else
  137. {
  138. echo json_encode(array('msg'=>'修改失败,请重试','success'=>false));exit;
  139. }
  140. }
  141. $arg_array = $arg_array[0];
  142. $study = $this->study->read($arg_array);
  143. $this->data['study'] = $study;
  144. $studyclass = $this->studyclass->find_all();
  145. $this->data['studyclass'] = $studyclass;
  146. $this->_Template('study_edit',$this->data);
  147. }
  148. public function _see($arg_array)
  149. {
  150. $arg_array = $arg_array[0];
  151. $study = $this->study->read($arg_array);
  152. $this->data['study'] = $study;
  153. $studyclass = $this->studyclass->find_all();
  154. $this->data['studyclass'] = $studyclass;
  155. $this->_Template('study_see',$this->data);
  156. }
  157. //删除
  158. public function _del()
  159. {
  160. $post = $this->input->post(NULL, TRUE);
  161. if(isset($post['s']))
  162. {
  163. $id_arr = $this->input->post('s');
  164. $id_arr = explode(',',$id_arr);
  165. if(!$id_arr)
  166. {
  167. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  168. }
  169. //循环删除记录
  170. foreach ($id_arr as $v)
  171. {
  172. $this->study->remove($v);
  173. }
  174. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  175. }
  176. }
  177. }