VladimirsBudkins

Example

Mar 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.66 KB | None | 0 0
  1. <?php
  2.  
  3. class Example extends CI_Controller {
  4.  
  5.     public function __construct() {
  6.         parent::__construct();
  7.         $this->load->model('ExampleModel');
  8.     }
  9.  
  10.     public function index() {
  11.         $data['records'] = $this->ExampleModel->findAll();
  12.         $this->load->view('example/index', $data);
  13.     }
  14.  
  15.     public function create() {
  16.         $this->_save(false);
  17.         $data['records'] = [];
  18.         $this->load->view('example/form', $data);
  19.     }
  20.  
  21.     public function update() {
  22.         $id = $this->uri->rsegnemt(n);
  23.         if (!$id) {
  24.             show_404();
  25.         }
  26.         $records = $this->ExampleModel->findBy($id);
  27.         if (!$records) {
  28.             show_404();
  29.         }
  30.         $this->_save($id);
  31.         $data['records'] = $records;
  32.         $this->load->view('example/form', $data);
  33.     }
  34.  
  35.     public function _save($id = false) {
  36.         $post = $this->input->post();
  37.         if ($post && $this->ExampleModel->validate($post)) {
  38.             $data = $this->ExampleModel->prepareData($post);
  39.             $insertId = $this->ExampleModel->save($data, $id);
  40.             redirect('example/update/' . $insertId);
  41.         }
  42.     }
  43.  
  44. }
  45.  
  46. class ExampleModel extends CI_Model {
  47.  
  48.     public function __construct() {
  49.         parent::__construct();
  50.     }
  51.  
  52.     public function validate($post) {
  53.        
  54.     }
  55.  
  56.     public function prepareData($post) {
  57.        
  58.     }
  59.  
  60.     public function save($data, $id = false) {
  61.         if (!$id) {
  62.             $this->db->insert('table', $data);
  63.             return $this->db->insert_id();
  64.         }
  65.         $this->db->update('table', $data, ['id' => $id]);
  66.         return $id;
  67.     }
  68.  
  69. }
Add Comment
Please, Sign In to add comment