A view is considered as a web page that function is to display all the elements of the user interface. In most cases, view is a fragment of a web page such as footer, widget areas, and sidebars. The most important function of view is that it can not be called directly.
You need to load the view using a controller. In this tutorial, I am going to show you the simple way to pass data in CodeIgniter application
1. Creating a View
To create a view you have to introduce a new text file and name it as wpblogview.php. Save the file in application > views > directory. Add the following code:
<html> <head> <title>Blog</title> </head> <body> <h1>Welcome to WPITECH Blog</h1> </body> </html>
2. Loading the View
Loading a view is usually executed through the following syntax:
To load a view you have to add the following code:
$this->load->view('name');
The name is the name of the view.
Now create a controller Blog.php file. This Blog.php file contains the method for loading the view.
<?php class Blog extends CI_Controller { public function index() { $this->load->view('wpblogview'); } }
After creating the controller, the URL for the view would be:
Your-domain.com/index.php/blog/
3. Passing the Array from Controller to View
Add the following code in your controller file
$data['mega_header'][] = (object) array('title' => 'portfolio image' , 'img' => 'https://complete path of image' ); $this->load->view('multiple_array', $data);
The objects are shown as an Arrow(->) and arrays as a Brick. You can easily access any object using the (->) arrow and an array with the Brick [‘..’].
Add the following snippet code in the view file:
<?php if (isset($mega_header)){ foreach ($mega_header as $key) { ?> <div class="header_item"> <img alt="<?php echo($key['title']); ?>" src="<?php echo($key->img); ?>"/> </div> <?php } } ?>
4. How to Load Multiple Views
CodeIgniter can easily handle multiple calls to the view ($this->load->view()) within a controller. Use the following code:
<?php class Page extends CI_Controller { public function index() { $data['page_title'] = 'title'; $this->load->view('header'); $this->load->view('menu'); $this->load->view('content', $data); $this->load->view('footer'); } }
5. Sorting Views in Subfolders
It is simple to sort subfolders and views:
$this->load->view(‘directory_name/file_name’);
6. Dynamic Data to Views
Data transfer from the controller to view using an array or object. Array or object passes as the second parameter of the load view method.
$data = array( 'title' => 'Title', 'heading' => 'Heading', 'message' => ' Message' ); $this->load->view('wpblogview', $data);
The controller file looks like this:
<?php class Blog extends CI_Controller { public function index() { $data['title'] = "Title"; $data['heading'] = "Heading"; $this->load->view('wpblogview', $data); } }
The view file looks like this:
<html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> </body> </html>
7. Creating the Loops
The data array that is passed to view files considered simple variable and multi-dimensional arrays. If there are multi-dimensional arrays, you have to set loops that generate multiple rows. Add the following code in the controller:
<?php class Blog extends CI_Controller { public function index() { $data['todo_list'] = array('First Object', 'Second Object', 'Third Object'); $data['title'] = "Title"; $data['heading'] = "Heading"; $this->load->view('wpblogview', $data); } }
Open the view file and create a loop:
<html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <ul> <?php foreach ($todo_list as $item):?> <li><?php echo $item;?></li> <?php endforeach;?> </ul> </body> </html>
8. Returning View
This method uses an optional third parameter and using this parameter the data returns as a string instead of sending it to a browser. It is one of the most important aspects if you need a dataset for processing. Now set the third parameter to TRUE will return the data. Add the following code
$string = $this->load->view('fileview', '', TRUE);