CodeIgniter
CodeIgniter is popular php web framework based on MVC (Model View Controller) Architecture.
What is MVC ?
In CodeIgniter Framework, the ‘html’ views, business logic controllers and data handling models are developed separately, enabling the designers to work with the views without any concern with the data and its handling; same goes for a programmer whose main focus is on developing the model.
Model
In PHP MVC Framework, the model is responsible for managing the data that involves the storage and retrieval of entities The model is a part of application that involves processing the data required by an application. The model part is unaware of the business logic and its corresponding view.
Controller
In CodeIgniter framework, the controller takes an input from user as a request, processes and analyzes this input, initializes and invokes the model, takes the model response and sends it back to the respected view. So the controller is a part of the application that involves the logical processes of the application also called the business logic. It acts as a middle layer for views and models.
View
In CodeIgniter framework, the view is responsible for all the display logic used to present data from the ‘model’ to the user. It passes the user input to a controller using control events. In short, a view is a part of an application that involves the creation of the HTML and interaction with a user.
Simple Example for view database data in a page
In Application folder, you can find the models, views and controller folders.
let's create a Controller in controller folder called Advertisement.php
--------------------------------------------------------------------------------------------------------
class Advertisement extends CI_Controller {
// constructor
public function _construct(){
parent::_construct();
}
public function show( ){
$this->load->helper('url');
//load the Advertisement_model model
$this->load->model("Advertisement_model");
$ads=$this->Advertisement_model->Advertisement();
$data['vehicle']=$car; //vehicle information
//load the views
$this->load->view("Advertisement_view",$data);
}
}
--------------------------------------------------------------------------------------------
- you have to create a class named Advertisement that is your class name
- So you can create methods in the class using function keyword
- In function, you can define which model you access and which view you view
- here I call the model Advertisement_model($this->load->model("Advertisement_model");) like this and Access the 'Advertisement' method to get the database data
- if you want you can call many models and views in one function
Let see the Model file.So create a Model in models folder called Advertisement_model.php
-------------------------------------------------------------------------------------------------------
- Here I have created a function in model it will get all data in the advertisment table in the database
- here you can get the data from Controller in vehicle array So you can access the array and get the data
- the view is called in the Advertisement function in the controller
class Advertisement_model extends CI_Model{
public function _construct(){
parent::_construct();
}
public function Advertisement( ){
$this->load->database();
$this->db->select("*");
$this->db->from("advertisements");
$query=$this->db->get();
return $query->result();
}
}
-------------------------------------------------------------------------------------------------------
Lets create a View called Advertisement_view.php in Views folder. and use this code.
here you limited result will show in the table
--------------------------------------------------------------------------------------------------------------------------
<div class="body" align="center" style='min-width:80%'>
<table>
< ? php
for ($i = 0; $i < count($vehicle); $i++) {
echo "<tr><td>".$vehicle[$i]->modelName."</td><td".$vehicle[$i]->year."></td><td>".$vehicle[$i]->category."</td><tr>";
? >
</table>
</div>
-------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment