《PHP教程:php編寫簡單的文章發(fā)布程序》要點:
本文介紹了PHP教程:php編寫簡單的文章發(fā)布程序,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP應用--
PHP應用-- 表的結構 `yi_article`
PHP應用--
PHP應用
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 ;
PHP應用article.php
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] 已經(jīng)存在,請輸入其它名稱!',
'min_lenght'=>' [%s] 最小長度為 [%s]',
'max_length'=>'[%s] 最大長度為 [%s]'
);
}
function index(){
echo "這里是文章的首頁";
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'>查看分頁</a>";
}
function article_list(){
echo "這里是文章列表";
//加載數(shù)據(jù)庫模型
//$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();
//第一步查詢出總記錄數(shù)
$this->load->database();
$config['total_rows']=$this->db->select('*')->from('article')->count_all_results();
//每頁記錄數(shù)
$config['per_page']=5;
//基礎url
$this->load->helper('url');
$config['base_url']=site_url('article/article_page');
//顯示的鏈接數(shù)
$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();
//第一步查詢出總記錄數(shù)
//$this->load->database();////
$config['total_rows']=$total_rows;
//每頁記錄數(shù)
$config['per_page']=$per_page;
//基礎url
$this->load->helper('url');////
$config['base_url']=site_url($base_url);
//顯示的鏈接數(shù)
$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');
//開始設置驗證規(guī)則
//set_message可以傳一個一維數(shù)組
$chinesetips=$this->tips;
$this->form_validation->set_message($chinesetips);
/*
$this->form_validation->set_message('required', ' [%s] 是必須填寫的!');
$this->form_validation->set_message('is_unique', '此 [%s] 已經(jīng)存在,請輸入其它名稱!');
$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','內(nèi)容','required');
$this->form_validation->set_rules('tag','狀態(tài)','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 "數(shù)據(jù)插入成功!";
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 "沒有傳遞參數(shù)";
exit;
}
$this->load->helper('url');
//從數(shù)據(jù)庫中查出來
$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');
//開始設置驗證規(guī)則
//set_message可以傳一個一維數(shù)組
$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','內(nèi)容','required');
$this->form_validation->set_rules('tag','狀態(tài)','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 "數(shù)據(jù)修改成功";
}else{
echo "數(shù)據(jù)修改失敗";
}
}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 "數(shù)據(jù)刪除成功!";
}else{
echo "數(shù)據(jù)刪除失敗!";
}
}
}
?>
PHP應用article_add.php
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 />
內(nèi)容:<input name="content" type="text" value="" /><br />
添加人:<input name="add_man" type="text" value="" /><br />
添加時間:系統(tǒ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>
PHP應用article_mod.php
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 />
內(nèi)容:<input name="content" type="text" value="<?php echo $content?>" /><br />
添加人:<input name="add_man" type="text" value="<?php echo $add_man;?>" /><br />
添加時間:系統(tǒ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>
PHP應用以上所述就是本文的全部內(nèi)容了希望大家能夠喜歡.
《PHP教程:php編寫簡單的文章發(fā)布程序》是否對您有啟發(fā),歡迎查看更多與《PHP教程:php編寫簡單的文章發(fā)布程序》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/10316.html