本文給大家分享的是使用php編寫(xiě)簡(jiǎn)單的文章發(fā)布程序,適合php菜鳥(niǎo)學(xué)習(xí)參考,希望對(duì)大家學(xué)習(xí)php能夠有所幫助。
--
表的結(jié)構(gòu) `yi_article`
- CREATE TABLE IF NOT EXISTS `yi_article` (
- `id` int(11) unsigned NOT NULL auto_increment,
- `title` varchar(256) NOT NULL,
- `content` mediumtext NOT NULL,
- `add_man` varchar(20) NOT NULL,
- `add_time` datetime NOT NULL,
- `views` int(11) NOT NULL,
- `tag` tinyint(4) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
article.php
- <?php
- class Article extends CI_Controller{
- public $tips;
- function __construct(){
- parent::__construct();
- //加載我自己的類庫(kù)
- $this->load->library('myclass');
- $this->load->database();
- $this->load->helper('url');
- $this->tips=array(
- 'required'=>' [%s] 是必須填寫(xiě)的!',
- 'is_unique'=>'此 [%s] 已經(jīng)存在,請(qǐng)輸入其它名稱!',
- 'min_lenght'=>' [%s] 最小長(zhǎng)度為 [%s]',
- 'max_length'=>'[%s] 最大長(zhǎng)度為 [%s]'
- );
- }
- function index(){
- echo "這里是文章的首頁(yè)";
- echo "<br />";
- //加載url輔助函數(shù)
- $this->load->helper('url');
- $addr=site_url('article/article_list');
- echo "<a href='$addr'>查看文章</a>";
- $addr=site_url('article/article_page');
- echo "<a href='$addr'>查看分頁(yè)</a>";
- }
- function article_list(){
- echo "這里是文章列表";
- //加載數(shù)據(jù)庫(kù)模型
- //$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();//當(dāng)然你可以用result()
- $this->myclass->p($info);
- echo "第一條記錄的標(biāo)題:".$info[0]['title'];
- echo "<br />";
- echo "第二條記錄的標(biāo)題:".$info[1]['id'];
- echo "<br />";
- echo "表article中共有這么些記錄:".$this->db->count_all('article');
- echo "<br />";
- echo "本次共查詢出這么些條記錄:".$query->num_rows();
- }
- function article_page($page=1){
- ///////////////////////////////////
- $config=array();
- //第一步查詢出總記錄數(shù)
- $this->load->database();
- $config['total_rows']=$this->db->select('*')->from('article')->count_all_results();
- //每頁(yè)記錄數(shù)
- $config['per_page']=5;
- //基礎(chǔ)url
- $this->load->helper('url');
- $config['base_url']=site_url('article/article_page');
- //顯示的鏈接數(shù)
- $config['num_links']=100;
- //在地址欄顯示當(dāng)前頁(yè)碼
- $config['use_page_numbers']=true;
- //定義首頁(yè)
- $config['first_link']='首頁(yè)';
- //定義末頁(yè)
- $config['last_link']='尾頁(yè)';
- //上一頁(yè)
- $config['prev_link']='上一頁(yè)';
- //下一頁(yè)
- $config['next_link']='下一頁(yè)';
- //把分頁(yè)包起來(lái)
- $config['full_tag_open']='<p>';
- $config['full_tag_close']='</p>';
- //第二步加載類庫(kù)
- $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();
- //echo base_url('abc/def');
- }
- protected function _page($total_rows,$per_page,$base_url){
- ///////////////////////////////////
- $config=array();
- //第一步查詢出總記錄數(shù)
- //$this->load->database();////
- $config['total_rows']=$total_rows;
- //每頁(yè)記錄數(shù)
- $config['per_page']=$per_page;
- //基礎(chǔ)url
- $this->load->helper('url');////
- $config['base_url']=site_url($base_url);
- //顯示的鏈接數(shù)
- $config['num_links']=100;
- //在地址欄顯示當(dāng)前頁(yè)碼
- $config['use_page_numbers']=true;
- //定義首頁(yè)
- $config['first_link']='首頁(yè)';
- //定義末頁(yè)
- $config['last_link']='尾頁(yè)';
- //上一頁(yè)
- $config['prev_link']='上一頁(yè)';
- //下一頁(yè)
- $config['next_link']='下一頁(yè)';
- //把分頁(yè)包起來(lái)
- $config['full_tag_open']='<p>';
- $config['full_tag_close']='</p>';
- //第二步加載類庫(kù)
- $this->load->library('pagination');
- $this->pagination->initialize($config);
- return $this->pagination->create_links();
- /////////////////////////////////////
- }
- function page($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;
- }
- function article_add(){
- $this->load->library('form_validation');
- //開(kāi)始設(shè)置驗(yàn)證規(guī)則
- //set_message可以傳一個(gè)一維數(shù)組
- $chinesetips=$this->tips;
- $this->form_validation->set_message($chinesetips);
- /*
- $this->form_validation->set_message('required', ' [%s] 是必須填寫(xiě)的!');
- $this->form_validation->set_message('is_unique', '此 [%s] 已經(jīng)存在,請(qǐng)輸入其它名稱!');
- $this->form_validation->set_message('min_length', ' [%s] 最小長(zhǎng)度為 [%s]');
- $this->form_validation->set_message('max_length', ' [%s] 最大長(zhǎng)度為 [%s]');
- */
- $this->form_validation->set_rules('title','標(biāo)題','trim|required|is_unique[article.title]|min_length[6]|max_length[12]');
- $this->form_validation->set_rules('content','內(nèi)容','required');
- $this->form_validation->set_rules('tag','狀態(tài)','required');
- if($this->form_validation->run()==true){
- echo "表單驗(yàn)證成功!";
- print_r($this->input->post());
- $data=$this->input->post();
- unset($data['Submit']);
- $data['add_time']=date('Y-m-d H:i:s');
- $data['views']='0';
- $st=$this->db->insert('article',$data);
- if($st){
- echo "數(shù)據(jù)插入成功!";
- echo "新的id為:".$this->db->insert_id();
- }
- //echo get_magic_quotes_gpc();
- }else{
- echo "表單驗(yàn)證失敗!";
- echo "<br />";
- echo validation_errors();
- }
- }
- function article_add_viewer(){
- $this->load->helper('url');
- $this->load->view('article_add');
- }
- function article_links(){
- $addr=site_url('article/article_mod_viewer/19');
- echo "<a href='$addr'>修改19</a>";
- }
- function article_mod_viewer($id){
- if($id==""){
- echo "沒(méi)有傳遞參數(shù)";
- exit;
- }
- $this->load->helper('url');
- //從數(shù)據(jù)庫(kù)中查出來(lái)
- $query=$this->db->select()->from('article')->where('id',$id)->get();
- $info=$query->row_array();
- print_r($info);
- $this->load->view('article_mod',$info);
- }
- function abc($val){
- $this->form_validation->set_message('abc','不行');
- //p($val);
- return true;
- }
- function article_mod(){
- $this->load->library('form_validation');
- //開(kāi)始設(shè)置驗(yàn)證規(guī)則
- //set_message可以傳一個(gè)一維數(shù)組
- $chinesetips=$this->tips;
- $this->form_validation->set_message($chinesetips);
- $this->form_validation->set_rules('title','標(biāo)題','trim|required|min_length[6]|max_length[12]|callback_abc');
- $this->form_validation->set_rules('content','內(nèi)容','required');
- $this->form_validation->set_rules('tag','狀態(tài)','required');
- if($this->form_validation->run()==true){
- echo "表單驗(yàn)證成功!";
- print_r($this->input->post());
- $data=$this->input->post();
- $id=$data['id'];
- unset($data['id']);
- unset($data['Submit']);
- $data['add_time']=date('Y-m-d H:i:s');
- $data['views']='0';
- //p($data);
- $st=$this->db->where('id',$id)->update('article',$data);
- if($st){
- echo "數(shù)據(jù)修改成功";
- }else{
- echo "數(shù)據(jù)修改失敗";
- }
- }else{
- echo "表單驗(yàn)證失敗!";
- echo "<br />";
- echo validation_errors();
- }
- }
- function article_del($id=''){
- if($id==""){
- //exit('請(qǐng)傳id');
- }
- $id=array(17,18,19);
- $this->db->where_in('id',$id)->delete('article');
- $st=$this->db->affected_rows();
- echo $st;
- if($st){
- echo "數(shù)據(jù)刪除成功!";
- }else{
- echo "數(shù)據(jù)刪除失敗!";
- }
- }
- }
- ?>
article_add.php
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <meta name="author" content="Www.XiaZaiBa.Com" />
- <title>無(wú)標(biāo)題 1</title>
- </head>
- <body>
- <form name="form1" action="<?php echo site_url('article/article_add')?>" method="post">
- 標(biāo)題:<input name="title" type="text" value="" /><br />
- 內(nèi)容:<input name="content" type="text" value="" /><br />
- 添加人:<input name="add_man" type="text" value="" /><br />
- 添加時(shí)間:系統(tǒng)自動(dòng)記錄<br />
- 狀態(tài):<input name="tag" type="radio" value="1" />顯示 <input name="tag" type="radio" value="0" />隱藏<br />
- <input type="submit" name="Submit" value="提交" />
- </form>
- </body>
- </html>
article_mod.php
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <meta name="author" content="Www.XiaZaiBa.Com" />
- <title>無(wú)標(biāo)題 1</title>
- </head>
- <body>
- <form name="form1" action="<?php echo site_url('article/article_mod')?>" method="post">
- 標(biāo)題:<input name="title" type="text" value="<?php echo $title;?>" /><br />
- 內(nèi)容:<input name="content" type="text" value="<?php echo $content?>" /><br />
- 添加人:<input name="add_man" type="text" value="<?php echo $add_man;?>" /><br />
- 添加時(shí)間:系統(tǒng)自動(dòng)記錄<br />
- 狀態(tài):<input name="tag" type="radio" value="1" <?php if($tag==1)echo 'checked';?> />顯示 <input name="tag" type="radio" value="0" <?php if($tag==0)echo 'checked';?> />隱藏<br />
- <input type="submit" name="Submit" value="提交" />
- <input type="hidden" value="<?php echo $id;?>" name="id" />
- </form>
- </body>
- </html>
以上所述就是本文的全部?jī)?nèi)容了希望大家能夠喜歡。
新聞熱點(diǎn)
疑難解答