ci3後臺文章的增刪改查
阿新 • • 發佈:2018-11-30
增
模型:
//過濾欄位 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 addArticle($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(){ //查詢欄目 $cates = $this->cate->get_cate(); $config['upload_path'] = './uploads/'; //注意:此路勁是相對於CI框架中的根目錄下的目錄 $config['allowed_types'] = 'gif|jpg|png'; //設定上傳的圖片格式 $config['max_size'] = '500'; //設定上傳圖片的檔案最大值 $config['max_width'] = '1200'; //設定圖片的最大寬度 $config['max_height'] = '1200'; $this->load->library('upload', $config); # 處理圖片上傳 #配置上傳相關引數 if ($this->upload->do_upload('pic')) { # 上傳成功,獲取檔名 $fileinfo = $this->upload->data(); $data['pic'] = $fileinfo['file_name']; #獲取表單提交資料 $data['title'] = $this->input->post('title'); $data['cateid'] = $this->input->post('cateid'); $data['content'] = $this->input->post('content'); $data['time'] = time(); if($this->input->post('is_recomend') == 'on'){ $data['is_recomend']=1; }else{ $data['is_recomend']=0; } if ($this->article->addArticle($data)) { redirect('article/lst'); } } $base_url = $this->config->item('base_url'); $this->load->view('article/add',array('base_url'=>$base_url,'cates'=>$cates)); }
檢視
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data"> <div class="form-group"> <label class="col-sm-2 control-label no-padding-right">文章標題</label> <div class="col-sm-6"> <input class="form-control" placeholder="" name="title" type="text"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label no-padding-right">所屬欄目</label> <div class="col-sm-6"> <select name="cateid"> <option value="">請選擇欄目</option> <?php foreach ($cates as $key => $val) {?> <option value="<?php echo $val['id']; ?>"><?php echo $val['title']; ?></option> <?php }?> </select> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label no-padding-right">文章縮圖</label> <div class="col-sm-6"> <input class="form-control" name="pic" id="file_upload" type="file" multiple="true"> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label no-padding-right">文章內容</label> <textarea id="newsEditor" name="content" ></textarea> <style> #newsEditor{ width:800px; margin-left: 200px; } </style> </div> <div class="form-group"> <label class="col-sm-2 control-label no-padding-right">是否推薦</label> <div class="col-sm-6"> <label> <input class="checkbox-slider colored-darkorange" name="is_recomend" type="checkbox"> <span class="text"></span> </label> </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_article', array('id' => $id));
}
控制器
//刪除
public function delete()
{
$id = $this->uri->segment(4, 0);
$res = $this->article->delete($id);
if ($res) {
redirect('article/lst');
} else {
redirect('article/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,$cateid,$pic,$content,$is_recomend,$time){
$query = $this->db->where(array('id'=>$id))->update('article',array('title'=>$title,'cateid'=>$cateid,'pic'=>$pic,'content'=>$content,'is_recomend'=>$is_recomend,'time'=>time()));
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->article->getOne($id);
//查詢欄目
$cates = $this->cate->get_cate();
$base_url = $this->config->item('base_url');
$this->load->view('article/edit',array('base_url'=>$base_url,'data'=>$data,'cates'=>$cates));
}
//執行修改
public function upd(){
$id=$_POST['id'];
$title=$_POST['title'];
$cateid=$_POST['cateid'];
$content=$_POST['content'];
$is_recomend=$_POST['is_recomend'];
if($is_recomend == 'on'){
$is_recomend = 1;
}else{
$is_recomend = 0;
}
$time=time();
$config['upload_path'] = './uploads/'; //注意:此路勁是相對於CI框架中的根目錄下的目錄
$config['allowed_types'] = 'gif|jpg|png';
//設定上傳的圖片格式
$config['max_size'] = '500'; //設定上傳圖片的檔案最大值
$config['max_width'] = '1200'; //設定圖片的最大寬度
$config['max_height'] = '1200';
$this->load->library('upload', $config);
if ($this->upload->do_upload('pic')) {
# 上傳成功,獲取檔名
$fileinfo = $this->upload->data();
$pic = $fileinfo['file_name'];
}
$result=$this->article->status($id,$title,$cateid,$pic,$content,$is_recomend,$time);
if($result){
redirect('article/lst');
}else{
redirect('article/edit');
}
}
檢視
<form class="form-horizontal" role="form" action="/admin.php/article/upd" method="post" enctype="multipart/form-data">
<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">
<label class="col-sm-2 control-label no-padding-right">所屬欄目</label>
<div class="col-sm-6">
<select name="cateid">
<option value="">請選擇欄目</option>
<?php foreach ($cates as $key => $val) {?>
<option <?php if ($val['id'] == $data['cateid']): ?> selected="selected" <?php endif; ?> value="<?php echo $val['id']; ?>"><?php echo $val['title']; ?></option>
<?php }?>
</select>
</div>
</div>
<div class="form-group">
<label
class="col-sm-2 control-label no-padding-right">文章縮圖</label>
<div class="col-sm-6">
<input id="pic" placeholder="" name="pic" style="display:inline;" type="file">
<?php if ($data['pic'] != ''): ?>
<img src="/uploads/<?php echo $data['pic']; ?>" height="50">
<?php else: ?>
<span>暫無縮圖</span>
<?php endif; ?>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right">文章內容</label>
<textarea id="newsEditor" name="content" ><?php echo $data['content']?></textarea>
<style>
#newsEditor{
width:800px;
margin-left: 200px;
}
</style>
</div>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right">是否推薦</label>
<div class="col-sm-6">
<label>
<input <?php if ($data['is_recomend'] == 1): ?> checked="checked"<?php endif; ?> class="checkbox-slider colored-darkorange" name="is_recomend" type="checkbox">
<span class="text"></span>
</label>
</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 lst()
{
$page_num = '3';//每頁的資料
$data= $this->Page_Model->page('blog_article',$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/article/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('article/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>
<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
if($val->cateid == 7){
echo "遊戲英雄";
}elseif ($val->cateid == 6){
echo "美女";
}elseif ($val->cateid == 5){
echo "超跑";
}
?>
</td>
<td align="center"><?php echo $val->title; ?></td>
<td align="center">
<?php if ($val-> pic != ''): ?>
<img src="/uploads/<?php echo $val->pic; ?>" height="50">
<?php else: ?>
暫無縮圖
<?php endif; ?>
</td>
<td align="center">
<?php if ($val-> is_recomend == 1): ?>
已推薦
<?php else: ?>
未推薦
<?php endif; ?>
</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>