php编写简单的文章发布程序
--
--表的结构`yi_article`
--
CREATETABLEIFNOTEXISTS`yi_article`( `id`int(11)unsignedNOTNULLauto_increment, `title`varchar(256)NOTNULL, `content`mediumtextNOTNULL, `add_man`varchar(20)NOTNULL, `add_time`datetimeNOTNULL, `views`int(11)NOTNULL, `tag`tinyint(4)NOTNULL, PRIMARYKEY(`id`) )ENGINE=MyISAMDEFAULTCHARSET=utf8AUTO_INCREMENT=21;
article.php
<?php
classArticleextendsCI_Controller{
public$tips;
function__construct(){
parent::__construct();
//加载我自己的类库
$this->load->library('myclass');
$this->load->database();
$this->load->helper('url');
$this->tips=array(
'required'=>'[%s]是必须填写的!',
'is_unique'=>'此[%s]已经存在,请输入其它名称!',
'min_lenght'=>'[%s]最小长度为[%s]',
'max_length'=>'[%s]最大长度为[%s]'
);
}
functionindex(){
echo"这里是文章的首页";
echo"<br/>";
//加载url辅助函数
$this->load->helper('url');
$addr=site_url('article/article_list');
echo"<ahref='$addr'>查看文章</a>";
$addr=site_url('article/article_page');
echo"<ahref='$addr'>查看分页</a>";
}
functionarticle_list(){
echo"这里是文章列表";
//加载数据库模型
//$this->load->model('article_model');
//$this->article_model->index();
//读取所有的文章
$this->load->database();
echo"<br/>";
$query=$this->db->where("id>",5)->select('id,title')->from('article')->order_by('id','desc')->limit(4)->get();
$info=$query->result_array();//当然你可以用result()
$this->myclass->p($info);
echo"第一条记录的标题:".$info[0]['title'];
echo"<br/>";
echo"第二条记录的标题:".$info[1]['id'];
echo"<br/>";
echo"表article中共有这么些记录:".$this->db->count_all('article');
echo"<br/>";
echo"本次共查询出这么些条记录:".$query->num_rows();
}
functionarticle_page($page=1){
///////////////////////////////////
$config=array();
//第一步查询出总记录数
$this->load->database();
$config['total_rows']=$this->db->select('*')->from('article')->count_all_results();
//每页记录数
$config['per_page']=5;
//基础url
$this->load->helper('url');
$config['base_url']=site_url('article/article_page');
//显示的链接数
$config['num_links']=100;
//在地址栏显示当前页码
$config['use_page_numbers']=true;
//定义首页
$config['first_link']='首页';
//定义末页
$config['last_link']='尾页';
//上一页
$config['prev_link']='上一页';
//下一页
$config['next_link']='下一页';
//把分页包起来
$config['full_tag_open']='<p>';
$config['full_tag_close']='</p>';
//第二步加载类库
$this->load->library('pagination');
$this->pagination->initialize($config);
echo$this->pagination->create_links();
/////////////////////////////////////
$page=$page?intval($page):1;
$start=($page-1)*$config['per_page'];
$query=$this->db->select('*')->from('article')->limit($config['per_page'],$start);
$info=$query->get()->result_array();
$this->myclass->p($info);
echo$this->pagination->create_links();
//echobase_url('abc/def');
}
protectedfunction_page($total_rows,$per_page,$base_url){
///////////////////////////////////
$config=array();
//第一步查询出总记录数
//$this->load->database();////
$config['total_rows']=$total_rows;
//每页记录数
$config['per_page']=$per_page;
//基础url
$this->load->helper('url');////
$config['base_url']=site_url($base_url);
//显示的链接数
$config['num_links']=100;
//在地址栏显示当前页码
$config['use_page_numbers']=true;
//定义首页
$config['first_link']='首页';
//定义末页
$config['last_link']='尾页';
//上一页
$config['prev_link']='上一页';
//下一页
$config['next_link']='下一页';
//把分页包起来
$config['full_tag_open']='<p>';
$config['full_tag_close']='</p>';
//第二步加载类库
$this->load->library('pagination');
$this->pagination->initialize($config);
return$this->pagination->create_links();
/////////////////////////////////////
}
functionpage($page=1){
$config['per_page']=5;
$page=$page?intval($page):1;
$start=($page-1)*$config['per_page'];
$query=$this->db->select('*')->from('article')->limit($config['per_page'],$start);
$info=$query->get()->result_array();
return$info;
}
functionarticle_add(){
$this->load->library('form_validation');
//开始设置验证规则
//set_message可以传一个一维数组
$chinesetips=$this->tips;
$this->form_validation->set_message($chinesetips);
/*
$this->form_validation->set_message('required','[%s]是必须填写的!');
$this->form_validation->set_message('is_unique','此[%s]已经存在,请输入其它名称!');
$this->form_validation->set_message('min_length','[%s]最小长度为[%s]');
$this->form_validation->set_message('max_length','[%s]最大长度为[%s]');
*/
$this->form_validation->set_rules('title','标题','trim|required|is_unique[article.title]|min_length[6]|max_length[12]');
$this->form_validation->set_rules('content','内容','required');
$this->form_validation->set_rules('tag','状态','required');
if($this->form_validation->run()==true){
echo"表单验证成功!";
print_r($this->input->post());
$data=$this->input->post();
unset($data['Submit']);
$data['add_time']=date('Y-m-dH:i:s');
$data['views']='0';
$st=$this->db->insert('article',$data);
if($st){
echo"数据插入成功!";
echo"新的id为:".$this->db->insert_id();
}
//echoget_magic_quotes_gpc();
}else{
echo"表单验证失败!";
echo"<br/>";
echovalidation_errors();
}
}
functionarticle_add_viewer(){
$this->load->helper('url');
$this->load->view('article_add');
}
functionarticle_links(){
$addr=site_url('article/article_mod_viewer/19');
echo"<ahref='$addr'>修改19</a>";
}
functionarticle_mod_viewer($id){
if($id==""){
echo"没有传递参数";
exit;
}
$this->load->helper('url');
//从数据库中查出来
$query=$this->db->select()->from('article')->where('id',$id)->get();
$info=$query->row_array();
print_r($info);
$this->load->view('article_mod',$info);
}
functionabc($val){
$this->form_validation->set_message('abc','不行');
//p($val);
returntrue;
}
functionarticle_mod(){
$this->load->library('form_validation');
//开始设置验证规则
//set_message可以传一个一维数组
$chinesetips=$this->tips;
$this->form_validation->set_message($chinesetips);
$this->form_validation->set_rules('title','标题','trim|required|min_length[6]|max_length[12]|callback_abc');
$this->form_validation->set_rules('content','内容','required');
$this->form_validation->set_rules('tag','状态','required');
if($this->form_validation->run()==true){
echo"表单验证成功!";
print_r($this->input->post());
$data=$this->input->post();
$id=$data['id'];
unset($data['id']);
unset($data['Submit']);
$data['add_time']=date('Y-m-dH:i:s');
$data['views']='0';
//p($data);
$st=$this->db->where('id',$id)->update('article',$data);
if($st){
echo"数据修改成功";
}else{
echo"数据修改失败";
}
}else{
echo"表单验证失败!";
echo"<br/>";
echovalidation_errors();
}
}
functionarticle_del($id=''){
if($id==""){
//exit('请传id');
}
$id=array(17,18,19);
$this->db->where_in('id',$id)->delete('article');
$st=$this->db->affected_rows();
echo$st;
if($st){
echo"数据删除成功!";
}else{
echo"数据删除失败!";
}
}
}
?>
article_add.php
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en">
<head>
<metahttp-equiv="content-type"content="text/html;charset=utf-8"/>
<metaname="author"content="Www.XiaZaiBa.Com"/>
<title>无标题1</title>
</head>
<body>
<formname="form1"action="<?phpechosite_url('article/article_add')?>"method="post">
标题:<inputname="title"type="text"value=""/><br/>
内容:<inputname="content"type="text"value=""/><br/>
添加人:<inputname="add_man"type="text"value=""/><br/>
添加时间:系统自动记录<br/>
状态:<inputname="tag"type="radio"value="1"/>显示<inputname="tag"type="radio"value="0"/>隐藏<br/>
<inputtype="submit"name="Submit"value="提交"/>
</form>
</body>
</html>
article_mod.php
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en">
<head>
<metahttp-equiv="content-type"content="text/html;charset=utf-8"/>
<metaname="author"content="Www.XiaZaiBa.Com"/>
<title>无标题1</title>
</head>
<body>
<formname="form1"action="<?phpechosite_url('article/article_mod')?>"method="post">
标题:<inputname="title"type="text"value="<?phpecho$title;?>"/><br/>
内容:<inputname="content"type="text"value="<?phpecho$content?>"/><br/>
添加人:<inputname="add_man"type="text"value="<?phpecho$add_man;?>"/><br/>
添加时间:系统自动记录<br/>
状态:<inputname="tag"type="radio"value="1"<?phpif($tag==1)echo'checked';?>/>显示<inputname="tag"type="radio"value="0"<?phpif($tag==0)echo'checked';?>/>隐藏<br/>
<inputtype="submit"name="Submit"value="提交"/>
<inputtype="hidden"value="<?phpecho$id;?>"name="id"/>
</form>
</body>
</html>
以上所述就是本文的全部内容了希望大家能够喜欢。