hi
codeigniter uses m-v-c i.e model view controller here i will give sample code how codeigniter works around mvc.
why codeigniter use the m-v-c architecture?
The back and front end of the application are separated and communicate using the controller,it will makes the code readable and easier to understand . codeigniter separate the design(view) and back end operations ,i.e insert,delete etc(model) and connect using the controller.
what is model ? Models have to do with connecting to a database and performing Create, Read, Update and Delete operations. Starting letter should be capital letter for the model class name where as class name equal to the file name .
what is controller ? A Controller is simply a class file that is named in a way that can be associated with a URI.
Create the controller file form.php and the class name of the controller must be same as the file name and initial letter of the class name must be start with upper case other wise it is not valid.
In below example the Form_model functions performed the INSERT and SELECT statements. The controller will call these functions to output what it needs to display to a view. All database-related functions in CodeIgniter are set and called using a model. The front end is displayed and managed by the view.
what is view? A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.
Views are never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing viewing.
Example I have database sample and containing table name articles , articles table have id and title , this sample code list out the data from the articles table and allow to add new one to the articles table through form.
The application folder present in the codeigniter
form_model.php (model)save this file in the application/models/form_model.php
Code:
<?php
class Form_model extends Model {
function Formmodel() {
parent::Model();
}
function submit_posted_data() {
$this->db->insert('articles',$_POST);
}
function get_all_data() {
$data['result']=$this->db->get('articles');
return $data['result'];
}
}
?>
save this file in the application/control/form.php
form.php(controller file )
Code:
<?php
class Form extends Controller {
function Form(){
parent::Controller();
$this->load->model('form_model', '', TRUE);
$this->load->database();
$this->load->helper(array('form','url'));
}
function index() {
$data['title']='Form Data';
$data['result'] = $this->form_model->get_all_data();
$this->load->view('forms_view',$data);
}
function submit() {
$this->form_model->submit_posted_data();
redirect('form');
}
}
?>
save this file in the application/view/forms_view.php
forms_view.php
Code:
<table border='1'>
<tr>
<th>ID</th>
<td>Article Title</td>
</tr>
<?php foreach($result->result_array() as $entry):?>
<tr>
<th><?php echo $entry['ArticleID'];?></th>
<td><?php echo $entry['ArticleTitle'];?></td>
</tr>
<?php endforeach;?>
</table>
<?php echo form_open('form/submit'); ?>
<br><br>
Titile<br>
<input type="text" name="ArticleID"><br>
Entry<br>
<input type="text" name="ArticleTitle">
<input type="submit" value="New">
</form>
after creating these three files open the browser and then type the url like as below
form is controller file name
Example:
http://www.example.com/index.php/form