Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Example extends CI_Controller {
- public function __construct() {
- parent::__construct();
- $this->load->model('ExampleModel');
- }
- public function index() {
- $data['records'] = $this->ExampleModel->findAll();
- $this->load->view('example/index', $data);
- }
- public function create() {
- $this->_save(false);
- $data['records'] = [];
- $this->load->view('example/form', $data);
- }
- public function update() {
- $id = $this->uri->rsegnemt(n);
- if (!$id) {
- show_404();
- }
- $records = $this->ExampleModel->findBy($id);
- if (!$records) {
- show_404();
- }
- $this->_save($id);
- $data['records'] = $records;
- $this->load->view('example/form', $data);
- }
- public function _save($id = false) {
- $post = $this->input->post();
- if ($post && $this->ExampleModel->validate($post)) {
- $data = $this->ExampleModel->prepareData($post);
- $insertId = $this->ExampleModel->save($data, $id);
- redirect('example/update/' . $insertId);
- }
- }
- }
- class ExampleModel extends CI_Model {
- public function __construct() {
- parent::__construct();
- }
- public function validate($post) {
- }
- public function prepareData($post) {
- }
- public function save($data, $id = false) {
- if (!$id) {
- $this->db->insert('table', $data);
- return $this->db->insert_id();
- }
- $this->db->update('table', $data, ['id' => $id]);
- return $id;
- }
- }
Add Comment
Please, Sign In to add comment