Load Multiple Views in one View – CodeIgniter

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.

Load Multiple Views in one View – CodeIgniter


Contents

  1. Create Multiple Views
  2. With View
  3. With Controller
  4. Conclusion

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 &copy; <?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>&nbsp;</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>&nbsp;</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.

If you found this tutorial helpful then don't forget to share.

5 thoughts on “Load Multiple Views in one View – CodeIgniter”

  1. Thanks Yogesh for taking the time to post this. I have been trying to figure this out for over a year now,, on and off. I understand your example. However, my problem is that part of my app must display possibly several hundred rows of data, each row consisting of simple object properties, but also must include two arrays of object properties.

    My difficulty is loading the $data array to these views and displaying them in some kind of ‘foreach’ loop structure. I’ve tried to place the foreach() in my main controller, in other controllers I created just for the iteration, as well as in a model or in the various views – but I can’t find a simple way to do this that works. It seems I’m stuck with a single huge view file which is very difficult to maintain or change.

    It would be great if you could expand a version of this tutorial that addressed the issue of iterating a set of views that must be loaded with data from a matching set of model methods that returned both properties and arrays of properties.

    Reply
  2. Hello one kind hint please

    In a CodeIgniter 4 same single view
    I need to display two html tables and they both need Codeigniter’s pagination

    One table should show the results of query e.g. Show the list of ToDo titles which is in a table called e.g. ‘todo’
    So this is query #1

    The other table should show the list of latest messages’ subjects from users , which are in a table called e.g. ‘users_messages’
    So this is query #2

    How is it suggested to handle this in “Codeigniter 4”?

    Thank you for any kind suggestion
    Alex

    Reply
    • Hi Alex,

      in your pagination method, pass second argument identifying which query is has been used for. Ex. paginate(10, ‘q1’) for first query and then paginate(10, ‘q2’), where 10 is defining how many rows you want to display and ‘q1’ defines the query group 1 and ‘q2’ defines second query group. Hope this helps.

      Thanks
      Chin

      Reply

Leave a Comment