|
本帖最后由 Mr.Xu 于 2013-2-8 11:23 编辑
小弟我第一次学习php,第一次用codeigniter编写demo,自己动手学习写东西总能够发现很多新知识。一下是小弟写的一个留言板的demo,请大家多多指教。
1、留言板的model类,与数据交互。
PHP复制代码
<?php
/**
* 描述:留言板model类
* author:xuzw13@gmail.com
* */
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();
}
/*query方式实现插入*/
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=?";
$query = $this->db->query($sql,$arrs);
return $this->db->affected_rows();
}
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复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* 描述:留言板controller类
* author:xuzw13@gmail.com
* */
class Guestbook extends CI_Controller {
public function __construct ()
{
parent ::__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 ()
{
header("Content-type:text/html;charset=utf-8");
$data['title'] = 'CI-留言板首页';
$this->load->view('guestbook/header',$data);
$this->load->view('guestbook/add');
$this->load->view('guestbook/footer.html');
}
/*添加留言数据处理*/
function add_guestbook ()
{
header("Content-type:text/html;charset=utf-8");
$data['title'] = 'CI-留言板首页';
$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)
{
header("Content-type:text/html;charset=utf-8");
$data['title'] = 'CI-留言板修改信息页面';
$result = $this->Guestbook_model->get_guestbook($_id);
$data['result'] = $result[0];
$this->load->view('guestbook/header',$data);
$this->load->view('guestbook/edit',$data);
$this->load->view('guestbook/footer.html');
}
/**
* 修改留言信息
* */
function edit_guestbook ()
{
header("Content-type:text/html;charset=utf-8");
$data['title'] = 'CI-留言板首页';
$this->form_validation->set_rules('title','标题','required');
$this->form_validation->set_rules('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/edit');
$this->load->view('guestbook/footer.html');
}
else
{
$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 */
复制代码
以上是简单留言板核心的代码,很多地方写的不好,请大家多多指教。
以下是源码,请大家多多指教!{:soso_e129:}{:soso_e129:}{:soso_e129:}
ci_guestbook.rar
(416.01 KB, 下载次数: 283)
demo图片
|
|