Php-ci Learning Note II (MVC)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.