小弟我第一次學習php,第一次用codeigniter編寫demo,自己動手學習寫東西總能夠發現很多新知識。一下是小弟寫的一個留言闆的demo,請大家多多指教。
1、留言闆的model類,與資料互動。
<?php
/**
* 描述:留言闆model類
* author:[email protected]
* */
class Guestbook_model extends CI_Model{
const TBL_GB = 'guestbook';
function __construct()
{
parent::__construct();
$this->load->database();
}
function getGuestbooks()
$sql = "SELECT * FROM ".self::TBL_GB;
$query = $this->db->query($sql);
//傳回條數
$numRow = $query->num_rows();
$resultRow = $query->result_array();
return array('numRow'=>$numRow,'resultRow'=>$resultRow);
function get_guestbook($_id)
$sql = "SELECT * FROM ".self::TBL_GB." where id=?";
$query = $this->db->query($sql,array($_id));
return $query->result_array();
/*query方式實作插入*/
function add_guestbook($arrs)
$sql = "insert into ".self::TBL_GB."(title,content,addtime) values (?,?,now())";
$query = $this->db->query($sql,$arrs);
return $this->db->affected_rows();
function add_guestbook_active($arrs)
$this->db->insert(self::TBL_GB,$arrs);
return $this->db->affected_rows();
function edit_guestbook($arrs)
$sql = "update ".self::TBL_GB." set title=?,content=? where id=?";
function delete_guestbook($_id)
$this->db->where('id',$_id);
$this->db->delete(self::TBL_GB);
}
/*End of guestbook_model.php */
/*Location: /application/models/guestbook_model.php */
2、留言闆的controller類,處理頁面請求等。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
* 描述:留言闆controller類
class Guestbook extends CI_Controller{
public function __construct()
$this->load->helper(array('form','url')); //加載輔助函數
$this->load->model('Guestbook_model');
$this->load->library('form_validation'); //表單驗證類
function index()
header("Content-type:text/html;charset=utf-8");
$data['title'] = 'CI-留言闆首頁';
$result = $this->Guestbook_model->getGuestbooks();
$data['guestbooks'] = $result;
$this->load->view('guestbook/header',$data);
$this->load->view('guestbook/index',$data);
$this->load->view('guestbook/footer.html');
/*添加留言界面*/
function add()
$data['title'] = 'CI-留言闆首頁';
$this->load->view('guestbook/add');
/*添加留言資料處理*/
function add_guestbook()
$this->form_validation->set_rules('title','Title','required');
$this->form_validation->set_rules('content','Content','required');
if($this->form_validation->run() == FALSE)
{
$data['title'] = 'CI-留言闆首頁';
$data['errors'] = validation_errors();
$this->load->view('guestbook/header',$data);
$this->load->view('guestbook/add');
$this->load->view('guestbook/footer.html');
}
else
/*$data = array($this->input->post('title'),$this->input->post('content'));
$result = $this->Guestbook_model->add_guestbook($data);*/
$data = array('title' => $this->input->post('title'),
'content' => $this->input->post('content'));
$result = $this->Guestbook_model->add_guestbook_active($data);
redirect(site_url('guestbook'));
/*修改頁面*/
function edit($_id)
$data['title'] = 'CI-留言闆修改資訊頁面';
$result = $this->Guestbook_model->get_guestbook($_id);
$data['result'] = $result[0];
$this->load->view('guestbook/edit',$data);
/**
* 修改留言資訊
* */
function edit_guestbook()
$this->form_validation->set_rules('title','标題','required');
$this->form_validation->set_rules('content','内容','required');
$this->load->view('guestbook/edit');
$data = array($this->input->post('title'),
$this->input->post('content'),
$this->input->post('hid'));
$result = $this->Guestbook_model->edit_guestbook($data);
redirect(site_url('guestbook'));
/*删除留言資訊*/
function delete($_id)
$result = $this->Guestbook_model->delete_guestbook($_id);
redirect(site_url('guestbook'));
/*End of guestbook.php */
/*Location: /application/controllers/guestbook.php */
以上是簡單留言闆核心的代碼,很多地方寫的不好,請大家多多指教。
以下是源碼,請大家多多指教!
<a href="http://blog.51cto.com/attachment/201302/112759305.png" target="_blank"></a>
本文轉自xuzw13 51CTO部落格,原文連結:http://blog.51cto.com/xuzhiwei/1133038,如需轉載請自行聯系原作者