Data passing from controller to view in codeigniter in PHP

 <?php

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

class User extends CI_Controller
{
    public function index()
    {
        //Below is the way for passing data from controller to View(Here view named "user")
        $data["title"] = "Code Improve. This code comes from User Controller";
        $data["fruits"] = array("Banana", "Orange", "Apple", "Pineapple");
        $this->load->view("user", $data);
    }

    public function add()
    {
        $this->load->view("user_add");
    }
}




Below is View File named user.php
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User view</title>
</head>

<body>
    <h1>This is User view</h1>
    <?php
    echo $title . "<br>";
    echo $fruits[0] . "<br>";
    echo $fruits[3] . "<br>";
    echo "<pre>";
    print_r($fruits);
    ?>

    <ul>
        <?php foreach ($fruits as $value) { ?>
            <li><?php echo $value; ?></li>
        <?php } ?>
    </ul>
</body>

</html>

Comments