Controller
Class article extends ci_controller{//constructor public function __construct () {parent::__construct (); Introduction of data Model $this->load->model (' Article/article_model ');//url handler function $this->load->helper (' Url_helper ');} The default function, Public function, index () {$data [' article ']= $this->article_model->get_article (); $this->load->view (' Templates/header ', $data); $this->load->view (' Article/index ', $data); $this->load->view (' templates/ Footer ', $data);} Aritcle/url functions Public Function view ($slug =null) {$data [' Article_item ']= $this->article_model->get_ Article ($slug); Returns 404if (Empty ($data [' Article_item '])) {show_404 () when no query result is found; $data [' title ']= $data [' Article_item '] [' title ']; $this->load->view (' Templates/header ', $data); $this Load->view (' Article/view ', $data); $this->load->view (' Templates/footer ', $data);} Public Function Create () {//form processing $this->load->helper (' form '); $this->load->library (' form_validation '); Verify the Acquired form data $this->form_validation->set_rulEs (' title ', ' title ', ' Required '), $this->form_validation->set_rules (' content ', "content", ' required '); $this- >form_validation->set_rules (' tag ', ' tag ', ' required ');//form validation different results return different pages if ($this->form_validation->run () ===false) {$this->load->view (' Templates/header ', $data); $this->load->view (' article/create ', $data), $this->load->view (' Templates/footer ', $data);} else{$this->article_model->set_article (); $this->load->view (' article/success ');}}}Model modifies the database connection in the application/config/database.php.
Class Article_model extends Ci_model{public function __construct () { //load Database$this->load->database ();} Check public Function get_article ($slug =false) { //No query parameters when if ($slug ===false) {$query = $this->db->get (' article ') ); return $query->result_array ();} $query = $this->db->get_where (' article ', Array (' id ' = = $slug)), return $query->row_array ();} Add Public Function Set_article () {$this->load->helper (' url ');//url_title is used to replace all spaces in a string with a connector (-) and convert all characters to lowercase $ Slug=url_title ($this->input->post (' title '), ' dash ', TRUE); $data =array (' title ' = = $this->input->post (' title '),//' slug ' = ' $slug, ' content ' = $this->input->post (' content '), return $this->db->insert ( ' article ', $data);}}
View
Validation error message echo validation_errors (); Call the Create method under the article Controller Echo form_open (' article/create '); ...
Php-ci Learning Note II (MVC)