增
模型
//过滤字段
protected function filter_field($data)
{
$fields = $this->db->list_fields($this->table_name);//获取数据库字段
foreach($data as $key=>$val){
if(!in_array($key,$fields)){
unset($data[$key]);
}
}
return $data;
}
//添加
public function addCate($data){
$data = $this->filter_field($data);
$res = $this->db->insert($this->table_name,$data);
if($res){
return $this->db->insert_id();
}else{
return false;
}
}
控制器
//添加
public function add()
{
if($_SERVER['REQUEST_METHOD'] == "POST"){
// 收集表单
$data = array(
'title' => $this->input->post('title'),
'time'=>time()
);
// 实例化模型,完成入库
$result = $this->cate->addCate($data);
if($result){
// 插入成功,使用下面的函数进行跳转
// 参数(控制器/方法)
redirect('cate/lst');
}else{
redirect('cate/add');
}
}
$base_url = $this->config->item('base_url');
$this->load->view('cate/add',array('base_url'=>$base_url));
}
视图
<form class="form-horizontal" role="form" action="" method="post">
<div class="form-group">
<label for="username" class="col-sm-2 control-label no-padding-right">栏目名称</label>
<div class="col-sm-6">
<input class="form-control" name="title" type="text">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">保存信息</button>
</div>
</div>
</form>
删
模型
//删除
public function delete($id)
{
return $this->db->delete('blog_cate', array('id' => $id));
}
控制器
//删除
public function delete()
{
$id = $this->uri->segment(4, 0);
$res = $this->cate->delete($id);
if ($res) {
redirect('cate/lst');
} else {
redirect('cate/delete');
}
}
视图
<a href="delete/id/<?php echo $val->id; ?>" class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 删除
</a>
改
模型
//修改
public function status($id,$title){
$query = $this->db->where(array('id'=>$id))->update('cate',array('title'=>$title));
return $query;
}
public function getOne($id){
return $this->db->where(array('id'=>$id))->get($this->table_name)->row_array();
}
控制器
//修改显示
public function edit()
{
$id = $this->uri->segment(4, 0);
$data=$this->cate->getOne($id);
$base_url = $this->config->item('base_url');
$this->load->view('cate/edit',array('base_url'=>$base_url,'data'=>$data));
}
//执行修改
public function upd(){
$id=$_POST['id'];
$title=$_POST['title'];
$result=$this->cate->status($id,$title);
if($result){
redirect('cate/lst');
}else{
redirect('cate/edit');
}
}
视图
<form class="form-horizontal" role="form" action="/admin.php/cate/upd" method="post">
<input type="hidden" name="id" value="<?php echo $data['id'] ?>"/>
<div class="form-group">
<label for="title"
class="col-sm-2 control-label no-padding-right">栏目名称</label>
<div class="col-sm-6">
<input class="form-control" name="title" value="<?php echo $data['title']?>"
type="text">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">保存信息</button>
</div>
</div>
</form>
查
模型
//查询
public function get_cate($slug = FALSE){
if ($slug === FALSE)
{
$query = $this->db->get('blog_cate');
return $query->result_array();
}
$query = $this->db->get_where('blog_cate', array('slug' => $slug));
return $query->row_array();
}
控制器
public function lst()
{
$page_num = '3';//每页的数据
$data= $this->Page_Model->page('blog_cate',$page_num,$this->uri->segment(3));
$total_nums=$data['total_nums']; //这里得到从数据库中的总页数
$data['query']=$data[0]; //把查询结果放到$data['query']中
$this->load->library('pagination');
$config['base_url'] = $this->config->base_url('admin.php/cate/lst');
$config['total_rows'] = $total_nums;//总共多少条数据
$config['per_page'] = $page_num;//每页显示几条数据
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$config['first_link'] = '首页';
$config['first_tag_open'] = '<span>';//“第一页”链接的打开标签。
$config['first_tag_close'] = '</span>';//“第一页”链接的关闭标签。
$config['last_link'] = '末页';//你希望在分页的右边显示“最后一页”链接的名字。
$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>';//“上一页”链接的打开标签。
$config['prev_tag_close'] = '</span>';//“上一页”链接的关闭标签。
$config['cur_tag_open'] = '<span class="current">';//“当前页”链接的打开标签。
$config['cur_tag_close'] = '</span>';//“当前页”链接的关闭标签。
$config['num_tag_open'] = '<span>';//“数字”链接的打开标签。
$config['num_tag_close'] = '</span>';
$this->pagination->initialize($config);
$base_url = $this->config->item('base_url');
$this->load->view('cate/lst',array('base_url'=>$base_url,'data'=>$data['query']));
}
视图
<div class="flip-scroll">
<table class="table table-bordered table-hover">
<thead class="">
<tr>
<th class="text-center">ID</th>
<th class="text-center">栏目名称</th>
<th class="text-center">操作时间</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $key => $val) {?>
<tr>
<td align="center"><?php echo $val->id; ?></td>
<td align="center"><?php echo $val->title; ?></td>
<td align="center"><?php echo date('Y-m-d H:i:s', $val->time); ?></td>
<td align="center">
<a href="edit/id/<?php echo $val->id; ?>" class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 编辑
</a>
<a href="delete/id/<?php echo $val->id; ?>" class="btn btn-primary btn-sm shiny">
<i class="fa fa-edit"></i> 删除
</a>
</td>
</tr>
<?php }?>
</tbody>
</table>
</div>
<div class="pagination">
<?php echo $this->pagination->create_links();?>
</div>