CodeIgniter Introductory Tutorial First: Information release, CI
First, MVC
CodeIgniter uses the MVC architecture: The control layer, the model layer, and the view layer.
The following folder corresponds to the application (Figure 1):
All new files end in. php
The View Layer View folder is placed in an HTML template
Model layer models store code for database operations
The control layer controllers the code that holds the logic judgment, obtains the data from the model layer and enters it into the view layer and sends it to the user.
Figure 1
features :
1. template Add Input Form
2. The controller increases the code that receives the form data and makes a simple check on the user input.
3. Output the title and body at the top of the form, and publish time.
Knowledge points used: CI helper classes (URLs) and input classes (inputs),
As well as CI ActiveRecord and passing values to the template.
Second, the initial configuration
1. Link 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 entry, which by default must access the control layer via index.php. For example, the Controllers folder has a class named Test, and test has a function called home,
The Access URL is: http://www.example.com/index.php/test/home
Three, Output page
1. Direct Output HTML Template
Create a new two files in the Controllers folder and the Views folder
test.php
Load->view (' Home ');} } home.php
Home This is our homepage.
The browser opens similar to the following address: Http://test.com/index.php/test/home
2. Inserting database Entries
Create a 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) <) or (strlen ($content) <) {echo ' title or text is too short '; return false;} $arr = array (' id ' = ' = ', ' title ' = = $title, ' content ' = $content, ' update_time ' + Time (), ' create_time ' + ti Me ()); $check = $this->news_model->insert ($arr, ' News '), if ($check) {redirect (' Test/home ');} Else{echo ' submit failed ';}}} home.php
<title>Home</title>This is our homepage.
news_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; }}}412DED80-4884-4A2F-AE37-6BA69CDC4278493498EE-0F5C-4676-9CEC-38E5A3F3E6FD 3. Querying the database and outputting news_model.php 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 modified to: Public Function Home () {$news = $this->news_model->get_all (' News '); $data [' news '] = $news; $this->load->view (' home ', $data); }
The body of the home template is modified to:
This is our homepage.
$value) {echo ''. $value [' title ']. '
Release time: '. Date (' y-m-d h:i:s ', $value [' Create_time ']). ''. $value [' content ']. '
';}? >
Refresh View Effect:
http://www.bkjia.com/PHPjc/1043451.html www.bkjia.com true http://www.bkjia.com/PHPjc/1043451.html techarticle CodeIgniter Introductory Tutorial First: Information Publishing, CI One, MVC codeigniter the MVC Architecture: Control layer, model layer, and view layer. The following folder corresponds to the application (Figure 1): ...