Home > Backend Development > PHP Tutorial > Day 6 ThinkPHP step-by-step quick website splicing (6)

Day 6 ThinkPHP step-by-step quick website splicing (6)

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:02:36
Original
1076 people have browsed it

        February 22, sunny day - haze. "Wrap walnuts with sweet-scented osmanthus stuffing, and the glutinous rice is like pearls from well water. I heard that Majia's rice flour is good, and they sell yuanxiao in the lantern style."

10. Backstage writing-continued-3

1, EditedWritten Actionindex method of module , as well as methods such as addition, deletion, modification and withdrawal.

<?php

class NewsAction extends Action {
	
	/**
	 * 	显示添加新闻主页面
	 */
	function index(){
		header("Content-Type:text/html; charset=utf-8");
		$this->assign('title','添加新闻');
		$this->assign('username',session('username'));
		if($id = (int)$_GET['id']){
			$news=M('News');
			$news_item=$news->where("id=$id")->find();
			$this->assign('news_item',$news_item);
			$this->assign('btn_ok_text','完成修改');
			$this->assign('btn_ok_act','update');
		}else{
			$this->assign('btn_ok_act','add');
			$this->assign('btn_ok_text','添加文章');
		}
		$this->display();
	}
	
	/**
	 * @函数	add
	 * @功能	新闻添加完成,写入数据库
	 */
	function add(){
		header("Content-Type:text/html; charset=utf-8");
	
		$News = D('News');
		if($News->create()){				
			$News->message=$_POST['editorValue'];
			$News->author=session('username');
				
			//将文章写入数据库
			if($News->add()){
				$this->success('新闻添加成功,返回上级页面',U('Index/index'));
			}else{
				$this->error('新闻添加失败,返回上级页面');
			}
				
		}else{
			$this->error($News->getError());
		}
	}
	
	/**
	 * @函数	quit
	 * @功能	登出账户,跳转至登录页面。并清除Session
	 */
	function quit(){
		session(null);//清空所有session信息
		redirect(U('/Login/index'),0, '重新登录');
	}
	
	/**
	 * @函数	delete
	 * @功能	删除文章
	 */
	function delete(){
		$news=M('news');
		if($news->delete($_GET['id'])){
			$this->success('文章删除成功');
		}else{
			$this->error($news->getLastSql());
		}
	}
	
	/**
	 * @函数	edit
	 * @功能	编辑文章
	 */
	function edit(){
		header("Content-Type:text/html; charset=utf-8");
		if($_GET['id']){
			redirect(U('/News/index/id/'.$_GET['id']),0, '编辑文章');
		}
	}
	
	/**
	 * @函数	update
	 * @功能	更新修改后的文章到数据库
	 */
	public function update(){
	
		header("Content-Type:text/html; charset=utf-8");
		$news=M('news');
	
		$data = array('subject'=>$_POST['subject'],'message'=>$_POST['editorValue'],'createtime'=>time(),'lastmodifytime'=>time());
		$id=$_POST['id'];
	
		$news->where('id='.$id)->setField($data); // 根据条件保存修改的数据
	     
		$this->success('新闻修改成功,返回上级页面',U('Index/index'));
	}
}
Copy after login


The above introduces the sixth day of ThinkPHP step-by-step quick website splicing (6), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template