CodeIgniter Laravel PHP Example HTML Javascript jQuery MORE
Need a Website Or Web Application.Contact : +91 9437911966 (Whatsapp) Note: Paid Service

Codeigniter View


In codeigniter view is a simple web page created using HTML. You can only call a view through a controller. It is used to show the user interface to the user. How to create a view:

Using your any text editor, create a file called about.php and paste the below code:

<html>
<head>
        <title>About Us</title>
</head>
<body>
        <h1>It is a tutorial site.</h1>
</body>
</html>

Then save the file in your application/views/ directory

Loading a View

To load a particular view file you will use the following method in controller:

$this->load->view('name');

Where name is the name of your view file.

The .php file extension does not need to be specified unless you use something other than .php.

Now, open the controller file you made earlier called Blog.php, and replace the echo statement with the view loading method:

<?php
class MySite extends CI_Controller {

        public function index()
        {
                $this->load->view('about');
        }
}

If you visit your site using the URL

example.com/index.php/mysite/

Output: It is a tutorial site.

Loading multiple views

In codeigniter you can load multiple view. See the below example.

<?php

class MySite extends CI_Controller {

        public function index()
        {
                $data['page_title'] = 'Your page title';
                $this->load->view('header');
                $this->load->view('content', $data);
                $this->load->view('footer');
        }

}

In the example above, we are using “dynamically added data”, which you will see below.

Storing Views file under sub-folder You can store your file inside a sub folder inside in the view folder.

$this->load->view('directory_name/file_name');

Here directory_name is your sub folder name.

Adding Dynamic Data to the View

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:

$data = array(
        'title' => 'My Title',
        'heading' => 'My Heading',
        'message' => 'My Message'
);

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

And here’s an example using an object:

$data = new Someclass();
$this->load->view('about', $data);

If you use an object, the class variables will be turned into array elements.

Let’s try it with your controller file. Open it add this code:

<?php
class Blog extends CI_Controller {

        public function index()
        {
                $data['title'] = "My Real Title";
                $data['heading'] = "My Real Heading";

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

Now open your view file and change the text to variables that correspond to the array keys in your data:

<html>
<head>
        <title><?php echo $title;?></title>
</head>
<body>
        <h1><?php echo $heading;?></h1>
</body>
</html>


Then load the page at the URL you’ve been using and you should see the variables replaced.