How to pass data from controller to view in codeignitor?

In this tutorial,I am going to show you pass data from codeignitor controller to codeignitor view.

Here is My Controller class.

<?php 



if ( ! defined('BASEPATH')) 

    exit('No direct script access allowed');

class test_controller extends CI_Controller {

    public function __construct()

       {

            parent::__construct();

       }



    public function index()

    {

        $data['user_id'] ="user" ;

        $data['display_data'] ="user data" ;

        $this->load->view('test_view',$data);

    }



   

}



My View File.

<!DOCTYPE html>

<html>

<body>



<h1>Sample Page</h1>



<p>Code</p>

<?php echo $user_id ;?>

<?php echo $display_data ;?>

</body>

</html>

All key values of $data array will be variable names in codeignitor view. Values of $data array vill be value of the variables in Codeignitor view.

If you want to get view files as $data, use following code

$string_data = $this->load->view('lode_view', '',true);

$data['page'] = $string_data;

$this->load->view('test_view',$data); 

Done !

0 comments:

Post a Comment

Ask anything about this Tutorial.