Contents

CodeIgniter3 設定 Model

Contents

其實這篇寫的是Model…
但我覺得我寫的最多應該會是DB Driver Reference
https://codeigniter.org.tw/userguide3/database/db_driver_reference.html?highlight=insert

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

class news_m extends CI_Model {

    public function __construct() {
        parent::__construct();
    }
    public function getData($id = '') {
        if ($id == ''):
            return $this->db->get('news');//SELECT * FROM news
        else:
            return $this->db->where('id', $id)->get('news');
        endif;
    }

    public function insert($post) {
        $this->db->insert('news', $post);
    }

    public function update($post) {
        $this->db->where('id', $post['id'])->update('news', $post);
    }

    public function delete($id) {
        $this->db->where('id', $id)->delete('news');
    }