Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //problem: can't load model in the controller constructor
- //url
- http://codeigniter/index.php/news/create/
- //routing
- $route['news/create'] = 'news/create';
- //controller
- <?php
- class News extends CI_Controller {
- function __construct()
- {
- parent::__construct();
- $this->load->model('news_model');
- }
- public function index(){
- $data['news'] = $this->news_model->get_news();
- $data['title'] = 'News archive';
- $this->load->view('templates/header', $data);
- $this->load->view('news/index', $data);
- $this->load->view('templates/footer', $data);
- }
- public function view($slug){
- $data['news_item'] = $this->news_model->get_news('$slug');
- if (empty($data['news_item'])) {
- show_404();
- }
- $data['title'] = $data['news_item']['title'];
- $this->load->view('templates/header', $data);
- $this->load->view('news/view', $data);
- $this->load->view('templates/footer', $data);
- }
- public function create(){
- $this->load->helper('form');
- $this->load->library('form_validation');
- $data['title'] = 'Create a news item';
- $this->form_validation->set_rules('title', 'Title', 'required');
- $this->form_validation->set_rules('text', 'text', 'required');
- if ($this->form_validation->run() === FALSE){
- $this->load->view('templates/header', $data);
- $this->load->view('news/create');
- $this->load->view('templates/footer');
- }else{
- $this->news_model->set_news();
- $this->load->view('news/success');
- }
- }
- }
- ?>
- //model
- <?php
- class News_model extends CI_Model
- {
- function __construct()
- {
- parent::__construct();
- $this->load->database();
- }
- }
- public function get_news($slug === FALSE){
- if ($slug === FALSE) {
- $query = $this->db->get('news');
- return $query->result_array();
- }
- $query = $this->db->get_where('news', array('slug' => $slug));
- return $query->row_array();
- }
- public function set_news(){
- $this->load->helper('url');
- $slug = url_title($this->input->post('title'), 'dash', TRUE);
- $data = array(
- 'title' => $this->input->post('title'),
- 'slug' => $slug,
- 'text' => $this->input->post('text')
- );
- return $this->db->insert('news', $data);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement