1. 程式人生 > >ci3管理員的增刪改查

ci3管理員的增刪改查

增:

模型

//過濾欄位
 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 addUser($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(
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password')),
        );

        // 例項化模型,完成入庫
        $this->load->model('User_Model', 'users');
        $result = $this->users->addUser($data);

        if($result){

        // 插入成功,使用下面的函式進行跳轉
        // 引數(控制器/方法)
            redirect('user/lst');
        }else{
            redirect('user/add');
        }
    }

    $base_url = $this->config->item('base_url');

    $this->load->view('user/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" id="username" placeholder="" name="username" type="text">
        </div>
    </div>
    <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" id="username" placeholder="" name="password" 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_user', array('id' => $id));

}

控制器
public function delete()
{

    $id = $this->uri->segment(4, 0);
    $res = $this->user->delete($id);

    if ($res) {
        redirect('user/lst');
    } else {
        redirect('user/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,$username,$password){
    $query = $this->db->where(array('id'=>$id))->update('user',array('username'=>$username,'password'=>md5($password)));
    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->user->getOne($id);

    $base_url = $this->config->item('base_url');

    $this->load->view('user/edit',array('base_url'=>$base_url,'data'=>$data));
}

//執行修改

public function upd(){
    $id=$_POST['id'];


    $username=$_POST['username'];
    $password=$_POST['password'];

    $result=$this->user->status($id,$username,$password);
    if($result){
        redirect('user/lst');
    }else{
        redirect('user/edit');
    }

}

檢視

<form class="form-horizontal" role="form" action="/admin.php/user/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" placeholder="" name="username" value="<?php echo $data['username']?>"
                    type="text">

        </div>
    </div>

    <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="password" value="<?php echo $data['password']?>"
                   type="password">

        </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>

模型(此處用的是分頁的模型)

<?php

defined('BASEPATH') OR exit('No direct script access allowed');


class Page_Model extends CI_Model{

    public function __construct()
    {
        parent::__construct();


    }


    public function page($tablename,$per_nums,$start_position){
        //傳入3個引數,表名字,每頁的資料量,起始位置
        $this->db->order_by('id','desc');
        $this->db->limit($per_nums,$start_position);
        $query=$this->db->get_where($tablename);
        $data=$query->result();
        $data2['total_nums']=$this->db->count_all($tablename);
        $data2[]=$data; //這裡大家可能看的優點不明白,可以分別將$data和$data2打印出來看看是什麼結果。
        return $data2;
    }
}

控制器

public function lst()
{


       $page_num = '3';//每頁的資料
       $data= $this->Page_Model->page('blog_user',$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/User/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('user/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>
        </tr>
        </thead>
        <tbody>
        <?php foreach ($data as $key => $val) {?>
            <tr>
                <td align="center"><?php echo $val->id; ?></td>
                <td align="center"><?php echo $val->username; ?></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>