Advertisement
oquidave

codeigniter_model_controller_error

Apr 5th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.16 KB | None | 0 0
  1. //problem: can't load model in the controller constructor
  2.  
  3. //url
  4. http://codeigniter/index.php/news/create/
  5.  
  6. //routing
  7.  
  8. $route['news/create'] = 'news/create';
  9.  
  10. //controller
  11.  
  12. <?php
  13. class News extends CI_Controller {
  14.    
  15.     function __construct()
  16.     {
  17.         parent::__construct();
  18.         $this->load->model('news_model');
  19.     }
  20.  
  21.     public function index(){
  22.         $data['news'] = $this->news_model->get_news();
  23.         $data['title'] = 'News archive';
  24.  
  25.         $this->load->view('templates/header', $data);
  26.         $this->load->view('news/index', $data);
  27.         $this->load->view('templates/footer', $data);
  28.     }
  29.  
  30.     public function view($slug){
  31.         $data['news_item'] = $this->news_model->get_news('$slug');
  32.         if (empty($data['news_item'])) {
  33.             show_404();
  34.         }
  35.  
  36.         $data['title'] = $data['news_item']['title'];
  37.  
  38.         $this->load->view('templates/header', $data);
  39.         $this->load->view('news/view', $data);
  40.         $this->load->view('templates/footer', $data);
  41.  
  42.     }
  43.  
  44.     public function create(){
  45.         $this->load->helper('form');
  46.         $this->load->library('form_validation');
  47.         $data['title'] = 'Create a news item';
  48.         $this->form_validation->set_rules('title', 'Title', 'required');
  49.         $this->form_validation->set_rules('text', 'text', 'required');
  50.  
  51.         if ($this->form_validation->run() === FALSE){
  52.             $this->load->view('templates/header', $data);
  53.             $this->load->view('news/create');
  54.             $this->load->view('templates/footer');
  55.         }else{
  56.             $this->news_model->set_news();
  57.             $this->load->view('news/success');
  58.         }
  59.     }
  60. }
  61.  
  62. ?>
  63.  
  64. //model
  65. <?php
  66.  
  67. class News_model extends CI_Model
  68. {
  69.    
  70.     function __construct()
  71.     {
  72.         parent::__construct();
  73.         $this->load->database();
  74.     }
  75. }
  76.  
  77. public function get_news($slug === FALSE){
  78.     if ($slug === FALSE) {
  79.         $query = $this->db->get('news');
  80.         return $query->result_array();
  81.     }
  82.     $query = $this->db->get_where('news', array('slug' => $slug));
  83.     return $query->row_array();
  84. }
  85.  
  86. public function set_news(){
  87.     $this->load->helper('url');
  88.     $slug = url_title($this->input->post('title'), 'dash', TRUE);
  89.  
  90.     $data = array(
  91.         'title' => $this->input->post('title'),
  92.         'slug' => $slug,
  93.         'text' => $this->input->post('text')
  94.          );
  95.     return $this->db->insert('news', $data);
  96. }
  97.  
  98.  
  99. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement