Sometimes it is required to create separate files that were common on all other pages like the header and footer section of the webpage.
This makes easier to maintain the pages.
include
statement is used to embed files in PHP.
In CodeIgniter, multiple views embedded in the single view either by controller or view.
Contents
1. Create Multiple Views
Creating multiple views.
Create header_view.php file in application/views/ directory.
<!doctype html> <html> <head> <style> /* Menu */ .menu { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: darkturquoise; } .menu li { float: left; } .menu li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } .menu li ul{ display: none; } .menu li a:hover { background-color: #2b90f5; } .active{ background: #2b90f5; } footer{ bottom: 0; position: absolute; border-top: 1px solid gray; width: 98%; padding: 20px 0px; text-align: center; } </style> </head> <body> <ul class='menu'> <li><a href="<?= site_url() ?>">Home</a></li> <li><a href="#">Aboutus</a></li> </ul>
Create footer_view.php file in application/views/ directory.
<footer> Copywrite © <?php echo date('Y'); ?> </footer> </body> </html>
2. With View
Loading header_view
and footer_view
views using $this->load->view()
method in home_view
view.
Create home_view.php file in application/views directory.
<?php // Load header_view $this->load->view('header_view'); ?> <h2><?= $content; ?></h2> <table> <tr> <td>Name : </td> <td><input type='text' /></td> </tr> <tr> <td>Username : </td> <td><input type='text' /></td> </tr> <tr> <td>Email : </td> <td><input type='text' /></td> </tr> <tr> <td> </td> <td><input type='button' value='Submit' /></td> </tr> </table> <?php // Load footer_view $this->load->view('footer_view'); ?>
Create a User.php file in application/controllers/ directory.
Loading home_view
view using $this->load->view()
method in index()
method.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User extends CI_Controller { public function __construct() { parent::__construct(); // load base_url $this->load->helper('url'); } public function index(){ $data['content'] = "Home Page"; // load view $this->load->view('home_view',$data); } }
3. With Controller
Using $this->load->view()
method to load multiple views and define in the order you want to display on the screen in the controller.
Create home_view.php file in application/views directory.
<h2><?= $content; ?></h2> <table> <tr> <td>Name : </td> <td><input type='text' /></td> </tr> <tr> <td>Username : </td> <td><input type='text' /></td> </tr> <tr> <td>Email : </td> <td><input type='text' /></td> </tr> <tr> <td> </td> <td><input type='button' value='Submit' /></td> </tr> </table>
Create a User.php file in application/controllers/ directory.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User extends CI_Controller { public function __construct() { parent::__construct(); // load base_url $this->load->helper('url'); } public function index(){ $data['content'] = "Home Page"; // load view $this->load->view('header_view'); $this->load->view('home_view',$data); $this->load->view('footer_view'); } }
4. Conclusion
Use either controller or view to load multiple views and declare them using $this->load->view()
method in the order you want to display on the screen.