接这次的学习,添加了分页
==========================================================================================================
最近研究CI框架,看了‘it不倒翁’的一个视频教程,大概了解一点。按照他的思路和做法弄了个很简单的留言板。自己添加了一个‘回复’的操作
直接上代码了。
借用‘it不倒翁’的一句话:在看视频教程前,先去看看手册。
四张页面,message_model.php->Model类文件,message.php->Controller控制器文件,message_index.php、message_edit.php两个view文件
message_model.php
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class message_model extends CI_Model
{
function __construct()
{
parent::__construct();
$this->table = 'message';
}
/**
* 查询该表的所有数据
*/
function getAll($limit=0, $offset=10)
{
//实现数据分页的两种方式
/**第一种:单独使用limit方法,传入数据量和偏移量
* $list = $this->db->limit($limit, $offset);
* $list = $this->db->get($this->table);
*/
/**
* 第二种方法:直接在get后边加数据条数和偏移量
*/
$list = $this->db->get($this->table, $limit, $offset);
return $list;
}
function getOne($m_id)
{
$this->db->where('m_id', $m_id);
$info = $this->db->get($this->table);
return $info;
}
/**
* 插入记录
*/
function insert($data)
{
$rs = $this->db->insert($this->table, $data);
return $rs;
}
/**
* 修改记录
*/
function update($data, $where)
{
$this->db->where('m_id', $where);
$rs = $this->db->update($this->table, $data);
return $rs;
}
/**
* 删除记录,单条的
*/
function delete($where)
{
$this->db->where('m_id', $where);
$rs = $this->db->delete($this->table);
return $rs;
}
function countTab($where=array())
{
return $this->db->count_all($this->table);
}
}
?>
message.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class message extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('message_model');
$this->load->database();
$this->load->library(array('table','pagination'));
}
function index($offset =0)
{
$limit = 4;//定义每页显示多少数据
$config['base_url'] = site_url('message/index');
$config['total_rows'] = $this->message_model->countTab();//数据总行数,此处需配合查询数据库
$config['per_page'] = $limit;//每页多少条记录
//自定义分页样式
$config['full_tag_open'] = '<p>';//该分页的自定义样式,这里放在p标签中
$config['full_tag_close'] = '</p>';
$config['first_link'] = '首页';//首页标签显示
$config['last_link'] = '尾页';
$config['first_tag_open'] = '<span>';//“第一页”链接的打开标签。
$config['first_tag_close'] = '</span>';//
$config['last_tag_open'] = '<span>';
$config['last_tag_close'] = '</span>';
$config['next_link'] = '>';//下一页链接显示内容
$config['next_tag_open'] = '<span>';
$config['next_tag_close'] = '</span>';
$config['prev_link'] = '<';//上一页链接显示内容
$config['prev_tag_open'] = '<span>';//上一页自定div样式
$config['prev_tag_close'] = '</span>';
$config['cur_tag_open'] = '<b>';//当前页自定义样式
$config['cur_tag_close'] = '</b>';
$config['num_tag_open'] = '<a>';//其他页码自定义样式
$config['num_tag_close'] = '</a>';
$config['anchor_class'] = " class=page ";//添加一个css
$this->pagination->initialize($config);
$data['limit'] = $limit;
$data['offset'] = $offset;
$data['page_now'] = $offset/$limit+1;
$data['pagination'] = $this->pagination->create_links();
$this->load->view('message_index', $data);
}
/**
* 插入一条新留言
*/
function insert()
{
//通过post获取数据,和设置初始值
$data = array(
'm_id' => '',
'm_name' => $this->input->post('m_name'),
'm_tit' => $this->input->post('m_tit'),
'm_content' => $this->input->post('m_content'),
'm_date_add' => time(),
'm_state' => 2,
);
$this->message_model->insert($data);
redirect(site_url());
}
/**
* 编辑已有的留言信息
*/
function edit()
{
if(!empty($_POST))
{
$data = array(
'm_name' => $this->input->post('m_name'),
'm_tit' => $this->input->post('m_tit'),
'm_content' => $this->input->post('m_content'),
);
$rs = $this->message_model->update($data, $this->uri->segment(3));
if($rs)
redirect(site_url('message/index/'.$this->uri->segment(3).'/'.$this->uri->segment(4)));
else
echo 'SQL执行失败!';
}
$this->load->view('message_edit');
}
/**
* 回复留言人
*/
function reply()
{
$where = $this->uri->segment(4);
$data = array(
'm_state' => 1,
'm_reply_id' => 1,
);
$rs = $this->message_model->update($data, $where);
if($rs)
redirect(site_url('message/index/'.$this->uri->segment(3).'/'.$where));
else
echo 'SQL执行失败!';
}
/**
* 删除一条数据
*/
function delete()
{
$where = $this->uri->segment(4);
$rs = $this->message_model->delete($where);
if($rs >0 || $rs === 0)
redirect(site_url('message/index/'.$this->uri->segment(3).'/'.$where));
else
echo 'SQL执行失败!';
}
}
?>
message_index.php //视图页面
<html>
<header>
<title>CI留言板</title>
</header>
<body>
<h1>CI 留言板</h1>
<hr />
<?php
$msgList = $this->message_model->getAll($limit, $offset);
if($msgList->num_rows() > 0)
{
//输出表头,ci集成的
echo $this->table->set_heading('序号','姓名','标题','内容','发表日期','当前状态','操作');
//遍历输出查询返回的数据
$i = 1;
foreach($msgList->result() as $row)
{
$reply = anchor(site_url('message/reply/'.$offset.'/'.$row->m_id), '回复').' ';
$edit = anchor(site_url('message/edit/'.$offset.'/'.$row->m_id), '编辑').' ';
$delete = anchor(site_url('message/delete/'.$offset.'/'.$row->m_id), '删除');
$row->m_date_add = date("Y-m-d m:i:s", $row->m_date_add);
$row->m_state = $row->m_state==1 ? "<span style='color:green;'>已回复</span>" : ($row->m_state == 2 ? "<span style='color:#993300;'>未回复</span>" : "<span style='color:red'>非法留言</span>");
$this->table->add_row($row->m_id, $row->m_name, $row->m_tit, $row->m_content, $row->m_date_add, $row->m_state, $reply.$edit.$delete);
$i++;
}
echo $this->table->generate();
echo $pagination;
}
echo '<hr />';
echo form_open('message/insert');
echo '姓名:'.form_input('m_name');
echo '<br />标题:'.form_input('m_tit');
echo '<br />内容:'.form_textarea('m_content');
echo '<br />';
echo form_submit('ins', '点击留言');
echo form_close();
?>
</body>
</html>
分页效果
(未分页效果):
文件在框架内的位置