CodeIgniter entry-level tutorials article 1: Information Publishing and ci_PHP tutorials

Source: Internet
Author: User
Tags php foreach
CodeIgniter: information publishing and ci. CodeIgniter entry-level first tutorial: Information Publishing. ci 1. MVCCodeIgniter adopts the MVC architecture: control layer, model layer, and view layer. Corresponding to the Application folder (): CodeIgniter Getting Started Tutorial Article 1: Information Publishing, ci

I. MVC

CodeIgniter adopts the MVC architecture: control layer, model layer, and view layer.

Corresponding to the folder () under the Application ():

All new files end with. php

View layer view folder in HTML template

Model layer model stores database operation code

The control layer controllers stores the code for logical judgment, obtains data from the model layer, inputs the data to the view layer, and sends it to the user.

Function:

1. add an input form to the template.

2. the controller adds code for receiving form data and performs simple verification on user input.

3. output the title, body, and release time at the top of the form.

Knowledge points used: CI helper class (url) and input class (input ),

And CI ActiveRecord and pass the value to the template.

II. initial configuration

1. link to the database

Modify database configuration:/application/config/database. php

  'hostname' => 'localhost',    'username' => 'root',    'password' => '',    'database' => 'test',    'dbdriver' => 'mysqli',    'dbprefix' => 'ts_',

  

2. modify the default route

The CI framework uses a single file Portal. by default, the access control layer must be accessed through index. php. For example, the controllers folder contains a class named test, and test has a function called home,

The access URL is: http://www.example.com/index.php/test/home

3. output page

1. directly output the HTML template

Create two files in the controllers folder and the views folder respectively.

Test. php

 Load-> view ('Home');} home. php
     
     HomeThis is our homepage

  

Open a URL similar to the following in a browser: http://test.com/index.php/test/home

2. Insert database entries

Create database table ts_news

Test. php

 Load-> helper ('URL'); $ this-> load-> model ('news _ Model');} public function home () {$ this-> load-> view ('Home');} public function add_news () {$ title = $ this-> input-> get ('title ', TRUE); $ content = $ this-> input-> get ('content'); if (strlen ($ title) <20) or (strlen ($ content) <20) {echo 'title or body content too short '; return false;} $ arr = array ('id' => '', 'title' => $ title, 'content' => $ content, 'update _ time' => time (), 'create _ time' => time ()); $ check = $ this-> news_model-> insert ($ arr, 'news'); if ($ check) {redirect ('test/home ');} else {echo 'submission failed' ;}} home. php
 
 HomeThis is our homepageNews_model.php
 Load-> database ();} public function insert ($ arr, $ table) {$ this-> db-> insert ($ table, $ arr ); if ($ this-> db-> affected_rows ()> 0) {return $ this-> db-> insert_id () ;}else {return FALSE ;}}} else 3. query the database and output News_model.php to add public function get_all ($ table) {$ this-> db-> select ('*'); $ query = $ this-> db-> get ($ table); $ query = $ query-> result_array (); return $ query;} Test. php's home is changed to: public function home () {$ news = $ this-> news_model-> get_all ('news'); $ data ['news'] = $ news; $ this-> load-> view ('Home', $ data );}

Modify the body of the Home Template:

This is our homepage
 $ Value) {echo'

'. $ Value ['title']. 'release date :'. date ('Y-m-d H: I: S', $ value ['create _ time']).'

'. $ Value ['content'].'

';}?>

  

  

Refresh and view results:

Http://www.bkjia.com/PHPjc/1043451.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1043451.htmlTechArticleCodeIgniter entry tutorial first article: information release, ci I, MVC CodeIgniter using MVC architecture that is: control layer, model layer and view layer. Corresponding Application folder ():...

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.