public function details($id = '') { if (!$id) { $this->error('参数错误', '/Home/Guide/doctor'); } $newsInfo = News::instance()->get($id, 'news'); $lists = News::instance()->lists('news', array('status' => 1, 'news_type_id' => $newsInfo['news_type_id'])); $count = count($lists); foreach ($lists as $k => $v) { if ($v['id'] == $id) { $newsK = $k; } } //计算上一页 $prevId = null; if ($newsK - 1 >= 0) { $prevId = $lists[$newsK - 1]['id']; } //计算下一页 $nextId = null; if ($newsK + 1 <= $count) { $nextId = $lists[$newsK + 1]['id']; } $this->assign('newsInfo', $newsInfo); $this->assign('prevId', $prevId); $this->assign('nextId', $nextId); $this->display(); }
/** * 添加编辑 */ public function save() { $id = I('id'); if ($_POST) { //判断标题,内容是否为空 if (!empty($_POST['title']) && !empty($_POST['content'])) { if (isset($id) && !empty($id)) { // 编辑 $news_icon_url = '.' . I('news_icon_url'); //新文件路径 $old_icon_url = '.' . I('old_icon_url'); //旧文件路径 //文件更改判断.删除旧文件 if ($news_icon_url != $old_icon_url && file_exists($old_icon_url)) { unlink($old_icon_url); } $data['updated_at'] = time(); $data['id'] = htmlspecialchars($_POST['id']); $data['title'] = htmlspecialchars($_POST['title']); $data['author'] = htmlspecialchars($_POST['author']); $data['description'] = htmlspecialchars($_POST['description']); $data['news_icon_url'] = htmlspecialchars($_POST['news_icon_url']); $data['news_type_id'] = htmlspecialchars($_POST['news_type_id']); $data['content'] = htmlspecialchars($_POST['content']); $data['status'] = htmlspecialchars($_POST['status']); News::instance()->edit($data, "news"); } else { // 添加 $data['created_at'] = time(); $data['updated_at'] = time(); $data['title'] = htmlspecialchars($_POST['title']); $data['author'] = htmlspecialchars($_POST['author']); $data['description'] = htmlspecialchars($_POST['description']); $data['news_icon_url'] = htmlspecialchars($_POST['news_icon_url']); $data['news_type_id'] = htmlspecialchars($_POST['news_type_id']); $data['content'] = htmlspecialchars($_POST['content']); $data['status'] = htmlspecialchars($_POST['status']); News::instance()->add($data, "news"); } } $this->_success('操作成功', U('News/lists')); exit; } if (isset($id) && !empty($id)) { // 编辑 $bean = News::instance()->get($id, "news"); $this->assign('bean', $bean); // 文章对象 $this->display('edit'); } else { $this->display('add'); } }