×

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly in the browser so, to reuse view file, we need to load or call it in the controller.

View Syntax

This is Abc.php file inside the view folder.

<!DOCTYPE
html>
 <html>
 <head>
 <meta charset = ”utf-8”>
 <title> My First Program </title.
 </head> 
 <body>
 CodeIgniter is a PHP framework for creating the web application.
 </body>
 </html> 

Loading method

A CodeIgniter provides a load() method to load the view in the Controller’s file. In this method, a view file is rendered to the Controller’ action method.

Syntax: $this-> load->view (‘filename’);

Structure of the View file

Now we will understand how we can create a view file in the views folder of the application.

Here we have written a few steps to create a view file inside the views folder.

  1. Firstly, you have to go to the View folder of the application’s folder.
Views

2. Then, Right-click on the views folder to create a subfolder. In the given below image, you will see there is a New Folder.

Note: The name of the subfolder can be the same as the controller’s file, or you can also define any name according to your choice.

Views2

3. After clicking on the New folder button, you will see the given below image. Now write the name of your folder in which you want to keep the view files. And press Enter button from your keyboard. Here, we are using the Users folder, and you can give the name of the folder according to your wish.

Views3

4. After creating the Users folder, right-click on it, then it will show the given below image. In this pop-up, there is an option New File to create the file name.

Views4

5. Now write the file name with .php extension. In the given image below, we have used the user.php file name inside the Users folder.

Views5

After creating the user.php file, now write the following code in the file.

<!DOCTYPE
html>
 <html>
 <head>
             <meta charset="utf-8">
             <title> Welcome to the Tutorials & Examples </title>
     </head> 
     <body>
     <h2 style="color:RED;" > CodeIgniter Framework </h2>
     <p style= "color:blue;"> CodeIhniter is an open source framework for developing a web application. It is based on MVC pattern </p>
     <h5 style= "color:Green;" > This is my First Example of View. </h5>
     </body>
     </html> 

Now, you have to create a Controller to call the view file in the controller’s action.

Create a Users.php file under the application/controllers/ and write the following code.

<?php
 class Users extends CI_Controller {
             public function first()
             {
         echo "This is my first Controller";  
         $this->load->view(Users/user');  // Users is the view folder in which we have passed //user.php file
     }
 }
 ?> 

$this->load->view() method is used to load the view file in the controller. So, we have to pass the folder and the file name of the view in the load method. In the above code, Users are the folder name, and the user is the file name of the view. You should not require to pass .php file extension in the load method.

Now execute the controller file to call a view file in the browser like localhost/CodeIniter-3.1.11/index.php/Users/first

Then it will show the below output:

Views6

Passing multiple views in the Controller

You can also pass the multiple view files in the same controller, as shown below.

Example

In this example, we have created two folders in views, which are Main and Users. Also, we have to create view files such as views/Main/main.php and the views/Users/user.php.

This is the main.php file

<!DOCTYPE
html>
 <html>
 <head>
             <meta charset="utf-8">
             <title>Welcome to the Tutorials & Examples</title>
     </head>
     <body> 
]     <h2 style="color:Yellow;"> This is my second view file </h2>
     <p style="color:Orange;"> You can create multiple view files and pass them to the controller</p>
     <h5 style="color:Purple;"> This is my Second Example of View. You can also design the view file according to your needs </h5> 
     <h3> //////////////////////////// </h3>
     </body>
     </html> 

This is the user.php file

<!DOCTYPE
<html>
 <html> 
 <head>
             <meta charset="utf-8">
             <title> Welcome to the Tutorials & Examples </title>
     </head>
     <body> 
     <h2 style="color:RED;" > CodeIgniter Framework </h2>
     <p style= "color:blue;"> CodeIhniter is an open source framework for developing a web application. It is based on MVC pattern </p>
     <h5 style= "color:Green;" > This is my First Example of View. </h5>
     </body>
     </html> 

Now, pass the above two view files in the Controller.

This is the Users.php file in the Controller.

<?php
 class Users extends CI_Controller {
             public function first()
             {
         echo "This is my first Controller";  
         $this->load->view(‘Users/user');  
        $this->load->view(‘Main/main’);
     }
 }
 ?> 

Now execute the above code in the browser like localhost/CodeIniter-3.1.11/index.php/Users/first

Then it will show the below output:

Views7

Related Topics

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

CodeIgniter Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

5 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.