Passing data from controller to view in codeigniter

  •  
  •  
  •  
  •  
  •  
  •  

Passing data from controller to view in codeigniterA view is  just a web page i.e combination of HTML and CSS also called front view, 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.) 

In codeigniter Views are never called directly,  Views must be loaded by a controller in an MVC framework, the Controller acts as a traffic cop, so it is responsible for fetching a particular view with certain data. 

 

In this post we will discuss about  Passing data from controller to view in codeigniter.

 

Creating a View

    Let’s start a simple blog site . So create 

     Header.php

    Menu.php 

     Body.php

     Footer.php

     

     Header.php

      

   

<html>

<head>
        <title>Technolila blog</title>
</head>

Menu.php 

  
<nav>   <li>Home</li><li>Blog</li> </nav>
 

  Body.php


<body>         <h1>Welcome to Technolila!</h1> </body>

Footer.php

<footer>    <p>Developed By: Technolila.com</p>     </footer>     </html>

 Now save the file in your application/views/ director  or you can save this file in a folder within views folder like main application/views/main.

you May Like :Top 5 Frameworks of PHP in 2020

Loading a View

   In codeigniter  you can load view by following method,

 

     $this->load->view('name');

 

The .php file extension does not need to be specified unless you use something other than .php.

 

Useful  Best OpenSource projects that developed using Codeigniter

Loading multiple views

 In codeigniter  we can load multiple view according to our needs by  $this->load->view() this method, they all appears in our front end page accordingly.

 

 For example

<?php
Class Tech extends CI_Controller {
 public function index()

        {
                $data['page_title'] = 'Technolila Blogs';

                $this->load->view('header');

                $this->load->view('menu');

                $this->load->view(‘body’, $data);

                $this->load->view('footer');

        }




}
?>

 

Storing Views within Sub-directories

  Like above we said , view file can be saved in views/main folder so for this  we  need to include the directory name loading the view      Example $this->load->view(‘directory_name/file_name’);     And above code  look like    

<?php   class Tech extends CI_Controller { 
 public function index(){ 
 $data['page_title'] = 'Technolila Blogs'; 
 $this->load->view('main/header'); 
 $this->load->view('main/menu'); 
 $this->load->view('main/body’, $data);   
 $this->load->view('main/footer');  
       }   } ?>

 

Adding Dynamic Data to the View

Generally in codeigniter data passed from controller to view  through array or object. Generally array or an object in the second parameter of the view loading method. Code looks like

 

$data = array(         'title' => 'Technolila',         'heading' => 'My Heading',         'message' => 'My Message' ); 
  $this->load->view(body, $data);

 

Let’s try it with Our controller file. Open it add this code:

 

<?php   class Tech extends CI_Controller {   
        public function index()  {
                 $data['page_title'] = 'Technolila Blogs';  
               $data['footertext'] = 'Power by Technolila.com'; 
                $data['menu'] = 'Home';
                 $data['content'] = 'Welcome to Technolila'; 
                $this->load->view('main/header');            
                $this->load->view('main/menu');               
               $this->load->view('main/body’, $data);         
               $this->load->view('main/footer');      
           }   } ?>

 

Now in  open view  file header,menu,body,footer

Header.php  

 

<html> <head>         <title><?php echo $page_title; ?></title> </head>

         

Menu.php   

 

<nav>   <li><?php echo $menu; ?></li> </nav>

 

Useful  How to Create Responsive Multi Level Dynamic Menu In Codeigniter

  Body.php

 

<body>         <h1><?php echo $content; ?></h1> </body>

 

Footer.php    

 

<footer>    <p><?php echo $footertext; ?></p>     </footer>     </html>

 

Creating Loops

Suppose we have more than one menu like home,blog,contact  then how can we pass menu variable . Here we need loops in view page.   Let’s see example

 

<?php   class Tech extends CI_Controller {         
  public function index()         {    
             $data['page_title'] = 'Technolila Blogs';  
               $data['footertext'] = 'Power by Technolila.com';   
              $data['menu'] = array('Home', 'Blog', 'Contact');   
              $data['content'] = 'Welcome to Technolila';              
              $this->load->view('main/header');               
              $this->load->view('main/menu'); 
                $this->load->view('main/body’, $data);   
              $this->load->view('main/footer');  
       }   } ?>

 

 

Now open menu.php and write   Menu.php   

 

<nav>   ?php foreach ($menu as $item){?>                   <li><?php echo $item;?></li>           <?php }?>   </nav>

Conclusion

in this post we discussed  about how to pass data to view in controller. This is very simple  you can try if you are  codeigniter beginner .

For any confusion you can comment we will answer as soon as possible.

 


Passing data from controller to view in codeigniter 2

Written By

vijay1983

Leave a Reply