×

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 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.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

6 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.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

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 Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 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 Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 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.

Structure of Codeigniter

The File structure of CodeIgniter is divided into three parts: ApplicationSystemuser_guide Application -: As the name represents Application, which contains all the main parts of the project like a controller, libraries, view, models, and others....

3 minutes read.

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 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 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 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.

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.