Ci framework (I), ci framework (
Ci directory structure
| ----- System Framework Program directory | ----- core program of the core framework | ----- CodeIgniter. php guided file | ----- Common. php loads public functions of the base class library | ----- Controller. php base controller file: CI_Controller | ----- Model. php base model file: CI_Model | ----- Config. php configuration file: CI_Config | ----- Input. php input file: CI_Input | ----- Output. php output file: CI_Output | ----- URL. php URL File: CI_URl | ----- Router. php route file: CI_Router | ----- Loader. php file loading class: CI_Loader | ----- helpers auxiliary function | ----- url_helper.php url-related auxiliary function, such: auxiliary Function for creating a url | ----- captcha_helper.php auxiliary function for creating a graphic Verification Code | ----- libraries common class library | ----- Pagination. php generic paging class library | ----- Upload. php generic File Upload class library | ----- Image_lib.php generic image processing class library | ----- Session. php common session class library | ----- language Pack | ----- database operation-related programs | ----- DB_active_rec.php quick operation file (ActiveRecord) | ----- fonts font | ----- application project directory | ----- core program of the core project | ----- helper function of the helpers project | ----- libraries common class library | ----- language Pack | ----- config project-related Configuration | ----- config. configuration file related to the php project | ----- database. configuration file related to the php database | ----- autoload. php sets to automatically load the configuration file of the class library | ----- constants. php constant configuration file | ----- routes. php route configuration file | ----- controllers controller directory | ----- welcome. the php controller file inherits the CI_Controller | ----- models model directory | ----- welcome_model.php model file and inherits the CI_Model | ----- views directory | ----- welcome. php view template file. The default suffix is. php | ----- cache file for storing data or Templates | ----- errors error message template | ----- hooks hook, extend the system functions without modifying the system core file | ----- third_party third-party library | ----- logs log | ----- index. php entry file
Access Form
Add the function in \ application \ controllers \ welcome. php:
public function hello() { echo "test"; }
Entry file. php/controller/Action
Create your own controller, hello. php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Hello extends CI_Controller { public function sayHello($name) { echo $name,",Hello World"; } }?>
Note:
- Method names starting with an underscore (_) cannot be accessed successfully and can only be accessed indirectly.
- Only public methods can be accessed.
- Try not to use the same method as the class name as a constructor.
Load View
Application \ views \ view_test.php or application \ views \ view \ test. php (this method is mainly to facilitate the management of views of the same type in the same folder)
Controller:
public function addView(){ $this->load->view("view_test");}
Or:
public function addView2(){ $this->load->view("view/test");}
Effect:
Allocate variable
Transfer data from the Controller to the view. The controller:
Public function addView () {$ this-> load-> vars ("title", "value"); $ list = array ('id' => 1, 'name' => 'jack', 'email '=> '2017 @ 123.com'), array ('id' => 2, 'name' => 'jack2 ', 'email '=> '2017 @ 123.com'), array ('id' => 3, 'name' => 'jack3 ', 'email '=> '2017 @ 123.com'); $ data ['new _ title'] = "title"; $ data ['LIST'] = $ list; $ this-> load-> vars ($ data); $ this-> load-> view ("view_test ");}
View:
Effect:
Public function addView () {$ this-> load-> vars ("title", "value"); $ list = array ('id' => 1, 'name' => 'jack', 'email '=> '2017 @ 123.com'), array ('id' => 2, 'name' => 'jack2 ', 'email '=> '2017 @ 123.com'), array ('id' => 3, 'name' => 'jack3 ', 'email '=> '2017 @ 123.com'); $ data ['new _ title'] = "title"; $ data ['LIST'] = $ list; $ this-> load-> vars ($ data); $ this-> load-> view ("view_test "); $ this-> load-> view ("footer ");}
View view_test:
View footer:
CI_hello_world!!!</body>
Display Effect:
Uri parameter acquisition
Controller:
public function getUri($id,$name,$year) { echo "id--->".$id."---name--->".$name."---year--->".$year."<br />"; echo "segment(1)--->".$this->uri->segment(1)."<br />"; echo "segment(2)--->".$this->uri->segment(2)."<br />"; echo "segment(3)--->".$this->uri->segment(3)."<br />"; echo "segment(4)--->".$this->uri->segment(4)."<br />"; echo "segment(5)--->".$this->uri->segment(5)."<br />"; }
Effect:
Load Database
This operation is implemented in the model in MVC.
Configure database parameters in \ application \ config \ database. php. Note:DbprefixAndSwap_preThese two parameters. In php, the prefix is written, which is treated as swap_pre by default, and then converted to dbprefix when put into the database, but it is best to make the two the same.
There is also $ active_group. The default value is default. If you want to connect two databases, name the default value separately, and specify the parameter in the function.
The data core class CI_Model must be inherited, And the constructor in the parent class must be reloaded.
class Model_name extends CI_Model{ function __construct() { parent::__construct(); }}
Each time you use a database, you need to load the database once:
$this->load->database();
For convenience, you can set the database to automatically load in \ application \ config \ autoload. php.
$autoload['libraries'] = array('database');
For database access objects, load them to the properties of the Super object $ this-> db
$ Res = $ this-> db-> query ($ SQL); // return object $ res-> result (); // return array, an array contains $ res-> result_array (); // returns a two-dimensional array, which is an associated array $ res-> row (); // returns the first data, directly an object
AR operation Database
In the database. php file, change $ active_recoed to TRUE to use AR.
// Query public function index () {$ res = $ this-> db-> get ('table name '); // The prefix foreach ($ res-> result () as $ item) {echo $ item-> name. "<br/> ";}}
// Insert public function index () {$ data = array ('name' => 'lisi', 'Password' => md5 ('lisi ')); $ bool = $ this-> db-> insert ("table name", $ data); var_dump ($ bool );}
// Update public function index () {$ data = array ('name' => 'hangzhou', 'Password' => md5 ('hangzhou ')); $ bool = $ this-> db-> update ('table name', $ data, array ('id' => 3); var_dump ($ bool );}
// Delete $ bool = $ this-> db-> delete ('table name', array ('id' => 2); var_dump ($ bool );
Let's take a look at the php ci framework development.
The general method is ul nesting, that is, nesting sub-menu ul in ul-li in the main menu. Two levels of loops are required.
First, loop through the main menu, you must have fixed conditions to determine the main menu, such as the main menu uid = 0 or other...
<Ul style = "margin-top: 20px; border-top: dashed 1px #666666;">
<Li style = "font-weight: 600;"> topic name </li>
<? Php foreach ($ news as $ news_item): // loop once
If ($ news_item ['uid'] = 0) {// judge and obtain the main menu
Echo "<li>". $ news_item ['title']. '<ul> ';
Foreach ($ news as $ child_item): // second loop
If ($ news_item ['id'] = $ child_item ['uid']) {// judge and obtain the corresponding sub-menu
Echo "<li>". "ss". $ child_item ['title']. "</li> ";
}
Endforeach;
Echo "</ul> </li> ";
}
Endforeach;?>
</Ul>
Of course, this is only limited to two levels of menu, multi-level or infinitus, you can use function Recursion
Function menu ($ uid = 0) {// sets the default value starting from the main menu
Global $ news;
Foreach ($ news as $ news_item ):
If ($ news_item ['uid'] = $ uid ){
Echo "<li>". $ news_item ['title']. '<ul> ';
Menu ($ news_item ['id']); // recursive call
Echo "</ul> </li> ";
}
Endforeach;
}
------ Call method ------------------------------
<Ul style = "margin-top: 20px; border-top: dashed 1px #666666 ;"
The CI framework wants to create a model class that inherits CI_Modle. I created it in app/core/MY_Model and reported an error directly.
Does MY_Controller have different class names?