News.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. class News 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_news','news');
  8. }
  9. public function _remap($arg,$arg_array)
  10. {
  11. if($arg == 'add')
  12. {
  13. $this->_add();
  14. }
  15. else if($arg == 'edit')
  16. {
  17. $this->_edit($arg_array);
  18. }
  19. else if($arg == 'del')
  20. {
  21. $this->_del();
  22. }
  23. else if($arg == 'rows')
  24. {
  25. $this->_rows();
  26. }
  27. else
  28. {
  29. $this->_index();
  30. }
  31. }
  32. //管理
  33. public function _index()
  34. {
  35. $post = $this->input->post(NULL, TRUE);
  36. if(isset($post['page']))
  37. {
  38. $page = $this->input->post('page',true);
  39. $perpage = $this->input->post('perpage',true);
  40. $title = $this->input->post('title',true);
  41. $timetk = $this->input->post('timetk',true);
  42. $timetj = $this->input->post('timetj',true);
  43. $timetk = strtotime($timetk);
  44. $timetj = strtotime($timetj);
  45. $where = "1=1 ";
  46. //数据排序
  47. $order_str = "id desc";
  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($timetk && $timetj)
  62. {
  63. $where .= " and ".$xztime." > '$timetk' and ".$xztime." < '$timetj'";
  64. }
  65. //取得信息列表
  66. $info_list = $this->news->find_all($where,'id,title,addtime',$order_str,$start,$perpage);
  67. //格式化数据
  68. foreach ($info_list as $key=>$value)
  69. {
  70. $info_list[$key]['addtime'] = date('Y-m-d H:i',$value['addtime']);
  71. }
  72. $total = $this->news->find_count($where);
  73. $pagenum = ceil($total/$perpage);
  74. $over = $total-($start+$perpage);
  75. $rows = array('total'=>$total,'over'=>$over,'pagenum'=>$pagenum,'rows'=>($info_list));
  76. echo json_encode($rows);exit;
  77. }
  78. $this->_Template('news',$this->data);
  79. }
  80. //添加
  81. public function _add()
  82. {
  83. $post = $this->input->post(NULL, TRUE);
  84. if(isset($post['title']))
  85. {
  86. $post['title'] = $this->input->post('title',true);
  87. $post['content'] = $this->input->post('content',true);
  88. $post['addtime'] = time();
  89. if($this->news->insert($post))
  90. {
  91. echo json_encode(array('msg'=>'添加成功','success'=>true));exit;
  92. }
  93. else
  94. {
  95. echo json_encode(array('msg'=>'添加失败,请重试','success'=>false));exit;
  96. }
  97. }
  98. $this->_Template('news_add',$this->data);
  99. }
  100. //修改
  101. public function _edit($arg_array)
  102. {
  103. $post = $this->input->post(NULL, TRUE);
  104. if(isset($post['title']))
  105. {
  106. $post['title'] = $this->input->post('title',true);
  107. $post['content'] = $this->input->post('content',true);
  108. if($this->news->save($post,$id))
  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. $arg_array = $arg_array[0];
  118. $news = $this->news->read($arg_array);
  119. $this->data['news'] = $news;
  120. $this->_Template('news_edit',$this->data);
  121. }
  122. //删除
  123. public function _del()
  124. {
  125. $post = $this->input->post(NULL, TRUE);
  126. if(isset($post['s']))
  127. {
  128. $id_arr = $this->input->post('s');
  129. $id_arr = explode(',',$id_arr);
  130. if(!$id_arr)
  131. {
  132. echo json_encode(array('msg'=>'参数错误!','success'=>false));exit;
  133. }
  134. //循环删除记录
  135. foreach ($id_arr as $v)
  136. {
  137. $this->news->remove($v);
  138. }
  139. echo json_encode(array('del'=>$id_arr,'msg'=>'删除记录成功!','success'=>true));
  140. }
  141. }
  142. }