PHP framework CodeIgniter getting started (2) _ PHP Tutorial

Source: Internet
Author: User
PHP framework CodeIgniter (2 ). PHP framework CodeIgniter getting started (2) This article reference links to introduce how CodeIgniter connects the Controller to the Model layer (operate the database) to implement the function of reading news entries. Using this document to get started with PHP framework CodeIgniter (2)

This document introduces how CodeIgniter connects the Controller to the Model layer (operate databases) to read news entries. In this article, we use collusion between Controller, Model, and View to control coordination.

1. create a Model

It should be clear that the database operations are at the model layer, rather than the Controller layer. the Controller is only responsible for the business control logic, and obtains data from the model and then sends it to the view. In phpmyadmin:

CREATE TABLE news (    id int(11) NOT NULL AUTO_INCREMENT,    title varchar(128) NOT NULL,    slug varchar(128) NOT NULL,    text text NOT NULL,    PRIMARY KEY (id),    KEY slug (slug));
Create tables. Note that the text type is UTF-8 encoded, and then two pieces of data are inserted at will.

Create News_model in 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 to return all the results queried, and row_array () is to return the current results of the query. For more information about the database, see the link.

2. create a Controller

News. php

Load-> model (news_model); $ this-> load-> helper ('URL _ helper ');}/*** show all news */public function index () {$ data ['new'] = $ 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 ');} /*** display a slug News * @ 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 ');}}
Note:

1. how does the Controller load the Model?

In the News constructor, load the model of the corresponding name in the model Directory through load-> model (''), and then call it through $ this-> news_model.

2. the Model name is case-insensitive, that is, the real model can be written in upper case, and the Model can be written in lower case during load.

3. how does the Controller interact with the view layer?

Load the files in the view folder through $ this-> load-> view ('') and pass an array. The key of the array in the Controller is the name of the corresponding variable in the view. For details about data transmission, refer to the template parsing class section of CI.

4. the code shows that the News controller loads index. php and view. php in the view/news folder.

3. create index. php

 

> View article

Note: Here the site_url is used to set the hyperlink. The intention is to enter news/slug in the address bar to directly jump to news/view/slug, so you need to set the route.

View. php

 '.$news_item['title'].'';echo $news_item['text'];

4. modify routes. php

On the basis of the original, add the following two sentences:

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

5. configure the database

Configure the database information in database. php.

After the above 5 steps, everything is OK.

Input in the browser: http: // localhost /~ Yanzi/CodeIgniter/index. php/news

Click the hyperlink and then http: // localhost /~ Yanzi/CodeIgniter/index. php/news/slug1111 to the following:


Refer (2) This article references how CodeIgniter connects the Controller to the Model layer (operate databases) to implement the function of reading news entries. Collusion through this article...

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.