PHP Framework CodeIgniter Getting Started with (2) _php tutorial

Source: Internet
Author: User
Tags php framework codeigniter

PHP Framework CodeIgniter Getting Started with (2)


This article provides a link to the CodeIgniter How to connect a controller to the model layer (Operational database) for reading the news entry function. Through this article collusion controller and model and how to coordinate between the view, control.

One, create model

One thing to be clear about is that the operation of the database is in the model layer, not the controller layer, and the controller is only responsible for the business control logic, taking the data from the model and giving it to view. In phpMyAdmin:

CREATE TABLE News (    ID int (one) not null auto_increment,    title varchar (+) NOT NULL,    slug varchar ($) Not NUL L,    text text not NULL,    PRIMARY key (ID),    key slug (slug));
Create a new table. Note the text type of the selected UTF8 encoding, and then randomly insert two data.

Create a new News_model under the Models folder:

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 ();}    }
Note that the above Result_array () is the return query to all results, and Row_array () is the current result of the returned query. Refer to the section on the database for links

Two new controller

news.php

Load->model (News_model);        $this->load->helper (' Url_helper ');    }    /**     * Show All news     *    /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 ');    }    /**     * shows a news of a slug     * @param null $slug     *    /Public Function view ($slug = null)    {        $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 ');}    }
Attention:

1,controller How to load model?

In the news constructor, the model in the model directory is loaded through Load->model ("), and then the time is invoked through $this->news_model.

2,model's name is not case-sensitive, that is, the real model can be capitalized, and can be written in lowercase when load.

How does 3,controller relate to the view layer?

Load the file under the View folder by $this->load->view ("), passing an array. The key of the array in the controller, under View, is the corresponding variable name. About passing data This section can refer to the template parsing class part of CI.

4, through the code can see, news this controller loaded the View/news folder under the index.php and view.php

Third, new index.php

>view article

Note: The Site_url is used here to set the hyperlink, the intention is that the Address bar input News/slug can jump directly to News/view/slug, so to set the route.

view.php

'. $news _item[' title ']. echo $news _item[' text '];

Four, modify routes.php

On the original basis, add the following two sentences:

$route [' news '] = ' news '; $route [' news/(: any) '] = ' news/view/$1 ';

Five, configure the database

Configure database-related information in database.php.

After the above 5 steps, everything OK.

Browser input: Http://localhost/~yanzi/CodeIgniter/index.php/news

After clicking on the hyperlink http://localhost/~yanzi/CodeIgniter/index.php/news/slug1111 go to the following:


http://www.bkjia.com/PHPjc/1065223.html www.bkjia.com true http://www.bkjia.com/PHPjc/1065223.html techarticle PHP Framework CodeIgniter Getting Started (2) the reference link in this article describes how CodeIgniter connects the controller to the model layer (Operational database) for reading the news entry function. Through this article collude ...

  • 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.