--
-- 表的結構 `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(); //加載我自己的類庫 $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]' ); } function index(){ echo "這裡是文章的首頁"; echo "<br />"; //加載url輔助函數 $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'>查看分頁</a>"; } function article_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(); } function article_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(); //echo base_url('abc/def'); } protected function _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(); ///////////////////////////////////// } 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'); //開始設置驗證規則 //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-d H:i:s'); $data['views']='0'; $st=$this->db->insert('article',$data); if($st){ echo "數據插入成功!"; echo "新的id為:".$this->db->insert_id(); } //echo get_magic_quotes_gpc(); }else{ echo "表單驗證失敗!"; 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 "沒有傳遞參數"; 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); } function abc($val){ $this->form_validation->set_message('abc','不行'); //p($val); return true; } function article_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-d H: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 />"; echo validation_errors(); } } function article_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
<!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>無標題 1</title> </head> <body> <form name="form1" action="<?php echo site_url('article/article_add')?>" method="post"> 標題:<input name="title" type="text" value="" /><br /> 內容:<input name="content" type="text" value="" /><br /> 添加人:<input name="add_man" type="text" value="" /><br /> 添加時間:系統自動記錄<br /> 狀態:<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>無標題 1</title> </head> <body> <form name="form1" action="<?php echo site_url('article/article_mod')?>" method="post"> 標題:<input name="title" type="text" value="<?php echo $title;?>" /><br /> 內容:<input name="content" type="text" value="<?php echo $content?>" /><br /> 添加人:<input name="add_man" type="text" value="<?php echo $add_man;?>" /><br /> 添加時間:系統自動記錄<br /> 狀態:<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>
以上所述就是本文的全部內容了希望大家能夠喜歡。