CodeIgniter getting started Article 1: Information Publishing

Source: Internet
Author: User
Tags php foreach codeigniter
: This article mainly introduces the first part of the CodeIgniter Getting Started Tutorial: Information Publishing. if you are interested in the PHP Tutorial, refer to it. 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.


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:

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:

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:

The first article of the Getting started course of CodeIgniter is described above: information release, including some content, and it is helpful to friends who are interested in PHP tutorials.

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