Passing Data From Controller to View in CodeIgniter 3

 <?php

defined("BASEPATH") or exit("No direct script access allowed");

class CodeImprove extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $data = array();
        $data["title"] = "Code Improve";
        $data["list"] = array("Ram", "Mahesh", "Rahul");
        $this->load->view("user_add", $data);   //Passing Data from Controller to View
        echo "This is CodeImprove Controller and index Function is present here...";
    }
    public function add()
    {
        $dataTwo = array();
        $dataTwo["titleTwo"] = "This is Code Improve Message";
        echo "This is CodeImprove Controller and add Function is present here...";
        $this->load->view("user", $dataTwo);    //Passing Data from Controller to View
    }
}





Below is View/user.php view File in CodeIgniter 3
<?php
defined("BASEPATH") or exit("No direct script access allowed");
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>User</title>
</head>

<body>
    <h2>This is User View File Page - <?php echo $titleTwo; ?> </h2>
</body>

</html>






Below is View/user_add.php view File in CodeIgniter 3
<?php
defined("BASEPATH") or exit("No direct script access allowed");
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>User Add</title>
</head>

<body>
    <div id="container">
        <h2>User Add Page - <?php echo $title; ?> </h2>
        <?php print_r($list); ?>
        <?php foreach ($list as $value) { ?>
            <ul>
                <li><?php echo $value; ?></li>
            </ul>
        <?php } ?>
    </div>
</body>

</html>



Comments