CodeIgniter编写的简单留言板的DEMO
本帖最后由 Mr.Xu 于 2013-2-8 11:23 编辑小弟我第一次学习php,第一次用codeigniter编写demo,自己动手学习写东西总能够发现很多新知识。一下是小弟写的一个留言板的demo,请大家多多指教。
1、留言板的model类,与数据交互。
<?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类,处理页面请求等。
<?phpif ( ! 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;
$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:}
学习了,谢谢楼主分享 不错,值得新手学习 基本的CRUD 不错,学习了 我是新手 发表于 2013-2-8 13:07 static/image/common/back.gif
基本的CRUD
是啊 ..基本的crud 新浪微博的雏形。。。哈哈哈哈哈 topwaybobo 发表于 2013-2-8 13:26 static/image/common/back.gif
不错,学习了
请多多指教 跟屁虫 发表于 2013-2-8 20:38 static/image/common/back.gif
新浪微博的雏形。。。哈哈哈哈哈
:P 很简单curd操作而已哈。。。 从代码来看,似乎任何人都可以删除任何人的评论吧? Altair 发表于 2013-2-8 23:44 static/image/common/back.gif
从代码来看,似乎任何人都可以删除任何人的评论吧?
恩。。没有做用户管理的。。。所以谁都可以 hcmtl 发表于 2013-2-9 10:28 static/image/common/back.gif
恩,刚刚会用..努力吧.。这东西很简单....
请多多指教
页:
[1]
2