CodeIgniter View
A view is a web page that displays all the elements of the User Interface. View deals with the front-end operations. View display data and capture user action. View send user action to controller. The location of all view files is applications/views.
View could not be called directly. You need to load the view through controller.
How to create view
Create a HTML page and name it firstview.php. Save this file inside applications/views.
Code of the firstview.php file.
1 2 3 4 5 6 7 8 |
<html> <head> <title>CodeIgniter View</title> </head> <body> <h1>Welcome to PHPGurukul Programming Blog</h1> </body> </html> |
Load the view
View will be load through controller. Syntax for loading view will be :
1 |
$this->load->view('name of the view'); |
For our case syntax will be :
1 |
$this->load->view('firstview'); |
Load multiple views in Controller
1 2 3 4 5 6 7 8 9 |
<?php class Blog extends CI_Controller { public function index() { $this->load->view('header'); $this->load->view('body'); $this->load->view('footer'); } } |
You can also create a new folder inside application/view and save your files in this folder.
Sort views in Subfolders
You can sort views within subfolders:
1 |
$this->load->view(‘directory_name/file_name’); |
Pass Dynamic Data to view
Data can be transfer from controller to view by using an array or object.
1 2 3 4 5 6 7 8 9 |
<?php class Blog extends CI_Controller { public function index() { $data['title'] = "CodeIgniter View"; $data['heading'] = "Welcome to PHPGurukul"; $this->load->view('firstview', $data); } } |
View will be look like this :
1 2 3 4 5 6 7 8 |
<html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> </body> </html> |