PHP Framework CodeIgniter Getting Started with (2)

Source: Internet
Author: User
Tags php foreach php framework codeigniter

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:

<?php/** * Created by Phpstorm. * User:yanzi * DATE:15/10/22 * Time: PM 6:38 */class News_model extends ci_model{public    function __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 ();}    }
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

<?php/** * Created by Phpstorm.     * User:yanzi * DATE:15/10/23 * time: Morning 11:38 */class News extends Ci_controller {/** * news constructor.        */Public Function __construct () {parent::__construct ();        $this->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_i        Tem '] = $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 '); }}
Note:

1,controller How to load model?

In the news constructor, the model is loaded in the Model directory by 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

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

<?phpecho ' 

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:





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP Framework CodeIgniter Getting Started with (2)

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.