Ci framework (I), ci framework (. Ci framework (I), ci framework (ci directory structure | ----- system Framework Program Directory | ----- core program of the core framework | ----- CodeIgniter. php guided file | ----- Common. php loaded Base Class Library 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:
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)
test_ci_hello_world
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:
test_ci_hello_world
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:
test_ci_hello_world
View footer:
CI_hello_world!!!
Display effect:
Uri parameter acquisition
Controller:
public function getUri($id,$name,$year) { echo "id--->".$id."---name--->".$name."---year--->".$year."
"; echo "segment(1)--->".$this->uri->segment(1)."
"; echo "segment(2)--->".$this->uri->segment(2)."
"; echo "segment(3)--->".$this->uri->segment(3)."
"; echo "segment(4)--->".$this->uri->segment(4)."
"; echo "segment(5)--->".$this->uri->segment(5)."
"; }
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."
";}}
// 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...
- Topic Name </li>
If ($ news_item ['uid'] = 0) {// judge and obtain the main menu
Echo"
- ". $ News_item ['title'].'
';
Foreach ($ news as $ child_item): // second loop
If ($ news_item ['id'] = $ child_item ['uid']) {// judge and obtain the corresponding sub-menu
Echo"
- "." 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"
- ". $ News_item ['title'].'
';
Menu ($ news_item ['id']); // recursive call
Echo "</ul> </li> ";
}
Endforeach;
}
------ Call method ------------------------------
>
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?
Middleware (1), ci framework (ci directory structure | ----- system Framework Program Directory | ----- core Framework Program | ----- CodeIgniter. php guided file | ----- Common. php loading Base Class Library...