Pass Data from Controller to View in CodeIgniter Framework

Pass Data from Controller to View in CodeIgniter


Set Base URL
Open config.php file in config folder. Set value for base_url config as below:

$config[‘base_url’] = ‘http://localhost:9092/LearnCodeIgniterRealApps/’;

Create Controller
Create new PHP file named demo.php in controllers folder as bellow:

<?php    define(‘BASEPATH’) OR exit(‘No direct script access allowed’); 

Class Demo extends CI_Controller{
     public Demo extends CI_Controller()
     {
          $data[‘age’] = 20;
          $data[‘username’] = ‘cyberteak’;
          $data[‘price’] = 4.5;
          $this->load->view(‘demo/index’, $data);
     }
}

Set Default Controller
Open routes.php file in config folder. Set value for default_controller as below:

$route[‘default_controller’] = ‘demo’;

Create View
Create new folder named demo in view folder. In this folder, created new PHP file named index.php as below:

<?php defined(‘BASEPATH’) or exit(‘No direct script access allowed’); ?>
<html>
     <head>
          <title>Demo page</title>
     </head>
     <body>
           <h3>Demo Page</h3>
           Age: <?php echo $age; ?>
          <br>
         Username: <?php echo $username; ?>
         <br>
         Price: <?php echo $price; ?>
     </body>
</html>

Run Application


Post a Comment

0 Comments