小弟我第一次学习php,第一次用codeigniter编写demo,自己动手学习写东西总能够发现很多新知识。一下是小弟写的一个留言板的demo,请大家多多指教。

1、留言板的model类,与数据交互。


  1. /** 
  2.  * 描述:留言板model类 
  3.  * author:[email protected] 
  4.  * */ 
  5. class Guestbook_model extends CI_Model{ 
  6.     const TBL_GB = 'guestbook'
  7.  
  8.     function __construct() 
  9.     { 
  10.         parent::__construct(); 
  11.         $this->load->database(); 
  12.     } 
  13.  
  14.     function getGuestbooks() 
  15.     { 
  16.         $sql = "SELECT * FROM ".self::TBL_GB; 
  17.         $query = $this->db->query($sql); 
  18.         //返回条数 
  19.         $numRow = $query->num_rows(); 
  20.         $resultRow = $query->result_array(); 
  21.         return array('numRow'=>$numRow,'resultRow'=>$resultRow); 
  22.     } 
  23.  
  24.     function get_guestbook($_id
  25.     { 
  26.         $sql = "SELECT * FROM ".self::TBL_GB." where id=?"
  27.         $query = $this->db->query($sql,array($_id)); 
  28.         return $query->result_array(); 
  29.     } 
  30.  
  31.     /*query方式实现插入*/ 
  32.     function add_guestbook($arrs
  33.     { 
  34.         $sql = "insert into ".self::TBL_GB."(title,content,addtime) values (?,?,now())"
  35.         $query = $this->db->query($sql,$arrs); 
  36.          return  $this->db->affected_rows(); 
  37.     } 
  38.  
  39.     /*query方式实现插入*/ 
  40.     function add_guestbook_active($arrs
  41.     { 
  42.         $this->db->insert(self::TBL_GB,$arrs); 
  43.         return  $this->db->affected_rows(); 
  44.     } 
  45.  
  46.     function edit_guestbook($arrs
  47.     { 
  48.         $sql = "update ".self::TBL_GB." set title=?,content=? where id=?"
  49.         $query = $this->db->query($sql,$arrs); 
  50.         return  $this->db->affected_rows(); 
  51.     } 
  52.  
  53.     function delete_guestbook($_id
  54.     { 
  55.         $this->db->where('id',$_id); 
  56.         $this->db->delete(self::TBL_GB); 
  57.     } 
  58.  
  59.  
  60. /*End of guestbook_model.php */ 
  61. /*Location: /application/models/guestbook_model.php */ 

2、留言板的controller类,处理页面请求等。

 


  1. if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
  2. /** 
  3.  * 描述:留言板controller类 
  4.  * author:[email protected] 
  5.  * */ 
  6. class Guestbook extends CI_Controller{ 
  7.  
  8.     public function __construct() 
  9.     { 
  10.         parent::__construct(); 
  11.         $this->load->helper(array('form','url'));   //加载辅助函数 
  12.         $this->load->model('Guestbook_model'); 
  13.         $this->load->library('form_validation'); //表单验证类 
  14.     } 
  15.  
  16.     function index() 
  17.     { 
  18.         header("Content-type:text/html;charset=utf-8"); 
  19.  
  20.         $data['title'] = 'CI-留言板首页'
  21.  
  22.         $result = $this->Guestbook_model->getGuestbooks(); 
  23.         $data['guestbooks'] = $result
  24.  
  25.         $this->load->view('guestbook/header',$data); 
  26.         $this->load->view('guestbook/index',$data); 
  27.         $this->load->view('guestbook/footer.html'); 
  28.     } 
  29.      
  30.     /*添加留言界面*/ 
  31.     function add() 
  32.     { 
  33.         header("Content-type:text/html;charset=utf-8"); 
  34.  
  35.         $data['title'] = 'CI-留言板首页';  
  36.          
  37.         $this->load->view('guestbook/header',$data); 
  38.         $this->load->view('guestbook/add'); 
  39.         $this->load->view('guestbook/footer.html'); 
  40.     } 
  41.  
  42.       /*添加留言数据处理*/ 
  43.     function add_guestbook() 
  44.     { 
  45.         header("Content-type:text/html;charset=utf-8"); 
  46.  
  47.         $data['title'] = 'CI-留言板首页';  
  48.          
  49.         $this->form_validation->set_rules('title','Title','required'); 
  50.         $this->form_validation->set_rules('content','Content','required'); 
  51.          
  52.         if($this->form_validation->run() == FALSE) 
  53.         { 
  54.             $data['title'] = 'CI-留言板首页';  
  55.             $data['errors'] = validation_errors();  
  56.             
  57.             $this->load->view('guestbook/header',$data); 
  58.             $this->load->view('guestbook/add'); 
  59.             $this->load->view('guestbook/footer.html'); 
  60.         } 
  61.         else 
  62.         { 
  63.             /*$data = array($this->input->post('title'),$this->input->post('content')); 
  64.             $result = $this->Guestbook_model->add_guestbook($data);*/ 
  65.             $data = array('title' => $this->input->post('title'), 
  66.                     'content' => $this->input->post('content'));    
  67.             $result = $this->Guestbook_model->add_guestbook_active($data);        
  68.             redirect(site_url('guestbook')); 
  69.         } 
  70.           
  71.     } 
  72.       
  73.     /*修改页面*/ 
  74.     function edit($_id
  75.     { 
  76.         header("Content-type:text/html;charset=utf-8"); 
  77.  
  78.         $data['title'] = 'CI-留言板修改信息页面';  
  79.  
  80.         $result = $this->Guestbook_model->get_guestbook($_id); 
  81.  
  82.         $data['result'] = $result[0]; 
  83.         $this->load->view('guestbook/header',$data); 
  84.         $this->load->view('guestbook/edit',$data); 
  85.         $this->load->view('guestbook/footer.html'); 
  86.  
  87.     } 
  88.  
  89.     /** 
  90.      * 修改留言信息 
  91.      * */ 
  92.     function edit_guestbook() 
  93.     { 
  94.         header("Content-type:text/html;charset=utf-8"); 
  95.  
  96.         $data['title'] = 'CI-留言板首页';  
  97.          
  98.         $this->form_validation->set_rules('title','标题','required'); 
  99.         $this->form_validation->set_rules('content','内容','required'); 
  100.          
  101.         if($this->form_validation->run() == FALSE) 
  102.         { 
  103.             $data['title'] = 'CI-留言板首页';  
  104.             $data['errors'] = validation_errors();  
  105.             $this->load->view('guestbook/header',$data); 
  106.             $this->load->view('guestbook/edit'); 
  107.             $this->load->view('guestbook/footer.html'); 
  108.         } 
  109.         else 
  110.         { 
  111.             $data = array($this->input->post('title'), 
  112.                           $this->input->post('content'), 
  113.                           $this->input->post('hid')); 
  114.  
  115.              $result = $this->Guestbook_model->edit_guestbook($data); 
  116.               
  117.               redirect(site_url('guestbook')); 
  118.         } 
  119.  
  120.     } 
  121.       
  122.     /*删除留言信息*/ 
  123.     function delete($_id
  124.     { 
  125.          $result = $this->Guestbook_model->delete_guestbook($_id); 
  126.               
  127.          redirect(site_url('guestbook')); 
  128.     } 
  129.  
  130. /*End of guestbook.php */ 
  131. /*Location: /application/controllers/guestbook.php */ 

以上是简单留言板核心的代码,很多地方写的不好,请大家多多指教。
以下是源码,请大家多多指教!