Passing Data From Model 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()
    {
        $this->load->model("User_Model");
        $userInfo = $this->User_Model->getRecord();
        // echo "<pre>"; print_r($userInfo); die;

        $data = array();
        $data["title"] = "Code Improve";
        $data["list"] = array("Ram", "Mahesh", "Rahul");
        $data["user_info"] = $userInfo;     //Passing Data from Model to View
        $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 File is Model/User_Model.php model File in CodeIgniter 3.
<?php
defined("BASEPATH") or exit("No direct script access allowed");

class User_Model extends CI_Model
{
    public function getRecord()
    {
        $responseArray = array("id" => 1, "name" => "Code Improve Website", "email" => "codeimprovewebsite4555@gmail.com");
        return $responseArray;
    }
}






Below File 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">
        Name: <?php echo $user_info["name"]; ?><br> <!--Data From Model to View-->
        ID: <?php echo $user_info["id"]; ?><br> <!--Data From Model to View-->
        Email: <?php echo $user_info["email"]; ?><br><br> <!--Data From Model to View-->
        <?php print_r($user_info); ?>
        <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