Recent frequent use of the CI framework, which is primarily about background development, focuses on sharing a little summary of the model and controller layers:
1. When it comes to interactive databases, we first add the connection information for the database we need to use in database.php, as in the following example (need to complete the relevant information):
$db [' name '] [' hostname '] = ';
$db [' name '] [' username '] = ';
$db [' name '] [' password '] = ';
$db [' name '] [' database '] = ';
$db [' name '] [' dbdriver '] = ';
$db [' name '] [' dbprefix '] = ';
$db [' name '] [' pconnect '] = FALSE;
$db [' name '] [' db_debug '] = FALSE;
$db [' name '] [' cache_on '] = FALSE;
$db [' name '] [' cachedir '] = ';
$db [' name '] [' char_set '] = ';
$db [' name '] [' dbcollat '] = ';
$db [' name '] [' swap_pre '] = ';
$db [' name '] [' autoinit '] = TRUE;
$db [' name '] [' stricton '] = FALSE;
2. Then in our model: $this->db= $this->load->database ("name", TRUE); This allows you to use our configured database (which can reference multiple databases) in the model.
Can be referenced in function __construct () or in a specific method;
3. Then we can write function to implement specific SQL functions, the following provides a relatively simple function connection database template:
function FuncName ($canshu 1, $canshu 2)
{
$sql = "Update tablename
Set column (column name) = column+1
where game_id =?
> Qudao_order? ";
$binds = Array (
$canshu 1,
$canshu 2
);
$is _true = $this->db->query ($sql, $binds); This is the format of implementing SQL
if ($is _true) {
return true;
} else return false;
}
4. Refer to model in controller, $this->load->model (' modelname '); You can use the method in model;
5. Use model in function of controller $return = $this->modelname->funcname ($canshu 1, $canshu 2);
Then continue to complete our function.
The above is a more direct way of access, we can combine PDO to access the database.
Put our database connection information in an array, so it's easier to add, and then use PDO to access, of course, you need to add the PDO module in PHP. The following are available for reference:
Public $db _info = Array (
' Test ' =>array (
' Host ' = ' 127.0.0.1 ',
' dbname ' = ' test ',
' Username ' = ' root ',
' Password ' = ' jxdisno1 '
)
' Other ' =>array (
)
);
Public Function Getconn ($DB)
{
$conn = new PDO ("mysql:host=". $this->db_info[$db] [' Host ']. '; Dbname= '. $this->db_info[$db] [' dbname '], $this->db_info[$db] [' username '], $this->db_info[$db] [' Password ' ]);
return $conn;
}
The interaction here with the Controller and view layer is not described here and will be described in detail next time. Here are some tips to share.
In the process of using the MVC framework, it is found that the development of the background is more and more simple and repetitive on the model and controller layers, but in the foreground there are more and more tasks, unless there are complex requirements on SQL.
The data interaction between view and controller layer is more diversified, the foreground needs to do more data processing than before.
So not backstage than the front-end development of tall, we will become more and more aware of the importance of foreground development and complexity.
Of course, I know too little and experience is not enough. I hope we can discuss and share with you.
The above describes a small summary of the use of the PHP CI framework, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.