×

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

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 Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

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 Calendaring Library

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

7 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 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 Benchmarking Class

The CodeIgniter contains the Benchmarking class that is used to calculate the time difference between two points or the memory usage of the passed statements in the application. By default, the Benchmarking...

7 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

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

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.

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 Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 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 Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 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 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 String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.