What is Views?

How to Load a view in codeigniter?

load a view in CodeIgniter, we use the built in Loader library.

File name :

$this->load->view('itechxpert', $data, true/false);

$this->load->view will tell CodeIgniter to look for itechxpert.php in the application/views directory, and display the contents of the file on the browser.

Note:

The CodeIgniter allows you to exclude the .php suffix from the view page.

File name :

The second parameter $data is optional and takes an associative array or object. This array/object is used to pass data to the view file, so it can be used or referenced within the view.

The final optional parameter determines whether the view's contents is displayed in the browser window, or returned as a string. This parameter defaults to false, displaying the content in the browser.


How to use view in codeigniter?

in CodeIgniter when a user requests something data from the browser then request goes to the codeigniter controller, controller process Your logic and controller handles the request and sends result to the view. The view is webpage which includes the HTML, CSS and PHP code.
your controller class handle the logic to get the data from the database and finally show the data on the view page.

File name :


Pass Data from codeigniter Controller to View.

1. First Create a View
2. Load the View
3. Pass data array from the controller to view.
4. you can Load Multiple Views
5. Load View from Sub directory
6. Add Dynamic Data to Views
7. Returning View as Data

First Create a View

File name : index.php

<html>
<head>
<title>Welcome itechxpert</title>
</head>
<body>
<h1>Codeigniter View</h1>
</body>
</html>

Load the View

File name :

$this->load->view('index');
// here index is the name of your view page. such as index.php

Controller

File name : Itechxpert.php

<?php

class Itechxpert extends CI_Controller {

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

}
?>

Pass array from the controller to view and display on view

File name : Itechxpert.php

<?php

class Itechxpert extends CI_Controller {

public function index() {
$data['page_details'] = array("title" => "itechxpert", "heading" => "Home Page");
$this->load->view('index', $data);
}

}
?>

show data on view page

we have the array page_details which holds the details. and added the second argument to the load view, load view takes the first argument as the name of the view, the second argument is the data which we send from the controller to the view.

File name :

<html>
<head>
<title><?php echo $page_details['title']; ?></title>
</head>
<body>
<h1><?php echo $page_details['heading']; ?> </h1>
</body>
</html>

Load Multiple Views

File name :

<?php

class Itechxpert extends CI_Controller {

public function index() {
$data['page_details'] = array("title" => "itechxpert", "heading" => "Rahatfoundation");
$this->load->view('header');
$this->load->view('sidebar');
$this->load->view('index', $data);
$this->load->view('footer');
}

}

Load View from Sub Directory

File name :

<?php

class Itechxpert extends CI_Controller {

public function index() {
$data['page_details'] = array("title" => "itechxpert", "heading" => "Home Page");
$this->load->view('pages/index', $data);
}

}

Add Dynamic Data to Views

File name :

<?php

class Itechxpert extends CI_Controller {

public function index() {
$data['page_details'] = array("title" => "itechxpert", "heading" => "Home page");
$data['user_details']=$this->Auth_model->get_user_details(); //This function returns the result array from the database
$this->load->view('pages/index', $data);
}

}

View page

File name : index.php

<html>
<head>
<title><?php echo $page_details['title']; ?></title>
</head>
<body>
<h1><?php echo $page_details['heading']; ?> </h1>

<table>
<thead>
<th>S.No</th>
<th>Name</th>
<th>Email</th>
<th>Mobile No.</th>
<th>Status</th>
</thead>
<tbody>
<?php
if (isset($user_details) && !empty($user_details)) {
$count = 1;
foreach ($user_details as $user) {
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td><?php echo $user['mobile_no']; ?></td>
<td><?php echo $user['status']; ?></td>
</tr>
<?php
$count++; }
} else {
?>
<tr><td colspan="4">No Data found.</td></tr>
<?php } ?>

</tbody>
</table>
</body>
</html>

Returning View as Data

$this->load->view(); method third optional parameter decides to load the view as a data, basically, this is used when you want to store view as a data.

File name :

<?php

class Itechxpert extends CI_Controller {

public function index() {
$data['page_details'] = array("title" => "Itechxpert", "heading" => "This is heading");
$page = $this->load->view('pages/index', $data, true);
}

}

Show data on view page

File name :

<html>
<head>
<title><?php echo $page_details['title']; ?></title>
</head>
<body>
<h1><?php echo $page_details['heading']; ?> </h1>

<table>
<thead>
<th>Name</th>
<th>Email</th>
<th>Mobile No.</th>
<th>Status</th>
</thead>
<tbody>
<?php
if (isset($user_details) && !empty($user_details)) {
$count = 1;
foreach ($user_details as $user) {
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $user->name; ?></td>
<td><?php echo $user->email; ?></td>
<td><?php echo $user->mobile_no; ?></td>
<td><?php echo $user->status; ?></td>
</tr>
<?php
$count++;}
} else {
?>
<tr><td colspan="4">No Data found.</td></tr>
<?php } ?>

</tbody>
</table>
</body>
</html>

File name :


File name :


Header

File name : header.php

<html lang="en">
<head>
<!-- SITE TITTLE -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BIGBAG Store - Ecommerce Bootstrap Template</title>
<!-- <?php //echo base_url('assets/css/bootstrap.css');?> -->
<!-- PLUGINS CSS STYLE -->
<link href="<?php echo base_url('assets/plugins/jquery-ui/jquery-ui.css');?>" rel="stylesheet">
<link href="<?php echo base_url('assets/plugins/bootstrap/css/bootstrap.min.css');?>" rel="stylesheet">
<link href="<?php echo base_url('assets/plugins/font-awesome/css/font-awesome.min.css');?>" rel="stylesheet">
<link href="<?php echo base_url('assets/plugins/selectbox/select_option1.css');?>" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/plugins/rs-plugin/css/settings.css');?>" media="screen">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/plugins/owl-carousel/owl.carousel.css');?>" media="screen">
<link rel="stylesheet" href="<?php echo base_url('assets/options/optionswitch.css');?>">
<link href='https://fonts.googleapis.com/css?family=Oxygen:400,300,700' rel='stylesheet' type='text/css'>
<link href="<?php echo base_url('assets/css/style.css');?>" rel="stylesheet">
<link rel="stylesheet" href="<?php echo base_url('assets/css/colors/default.css');?>" id="option_color">

</head>

<body>


Index

File name : index.php

<?php include 'header.php'; ?>
<!-- BANNER -->
<div class="bannercontainer">
<div class="fullscreenbanner-container">
<div class="fullscreenbanner">
<ul>
<li data-transition="slidehorizontal" data-slotamount="5" data-masterspeed="700" data-title="Slide 1">
<img src="img/home/banner-slider/slider-bg.jpg" alt="slidebg1" data-bgfit="cover" data-bgposition="center center" data-bgrepeat="no-repeat">
<div class="slider-caption container">
<div class="tp-caption rs-caption-1 sft start"
data-hoffset="0"
data-x="370"
data-y="54"
data-speed="800"
data-start="1500"
data-easing="Back.easeInOut"
data-endspeed="300">
<img src="img/home/banner-slider/shoe1.png" alt="slider-image">
</div>

<div class="tp-caption rs-caption-2 sft"
data-hoffset="0"
data-y="119"
data-speed="800"
data-start="2000"
data-easing="Back.easeInOut"
data-endspeed="300">
Canvas Sneaker
</div>

<div class="tp-caption rs-caption-3 sft"
data-hoffset="0"
data-y="185"
data-speed="1000"
data-start="3000"
data-easing="Power4.easeOut"
data-endspeed="300"
data-endeasing="Power1.easeIn"
data-captionhidden="off">
Exclusive to <br>
BigBag <br>
<small>Spring / Summer 2016</small>
</div>
<div class="tp-caption rs-caption-4 sft"
data-hoffset="0"
data-y="320"
data-speed="800"
data-start="3500"
data-easing="Power4.easeOut"
data-endspeed="300"
data-endeasing="Power1.easeIn"
data-captionhidden="off">
<span class="page-scroll"><a target="_blank" href="http://goo.gl/VCbeOp" class="btn primary-btn">Buy Now<i class="glyphicon glyphicon-chevron-right"></i></a></span>
</div>
</div>
</li>
<li data-transition="slidehorizontal" data-slotamount="5" data-masterspeed="1000" data-title="Slide 2">
<img src="img/home/banner-slider/slider-bg.jpg" alt="slidebg" data-bgfit="cover" data-bgposition="center center" data-bgrepeat="no-repeat">
<div class="slider-caption container captionCenter">
<div class="tp-caption rs-caption-1 sft start captionCenterAlign"
data-x="center"
data-y="228"
data-speed="800"
data-start="1500"
data-easing="Back.easeInOut"
data-endspeed="300">
<img src="img/home/banner-slider/shoe2.png" alt="slider-image">
</div>

<div class="tp-caption rs-caption-2 sft captionCenterAlign"
data-x="center"
data-y="50"
data-speed="800"
data-start="2000"
data-easing="Back.easeInOut"
data-endspeed="300">
Exclusive to BigBag
</div>

<div class="tp-caption rs-caption-3 sft captionCenterAlign"
data-x="center"
data-y="98"
data-speed="1000"
data-start="3000"
data-easing="Power4.easeOut"
data-endspeed="300"
data-endeasing="Power1.easeIn"
data-captionhidden="off">
Canvas Sneaker
</div>

<div class="tp-caption rs-caption-4 sft captionCenterAlign"
data-x="center"
data-y="156"
data-speed="800"
data-start="3500"
data-easing="Power4.easeOut"
data-endspeed="300"
data-endeasing="Power1.easeIn"
data-captionhidden="off">
<span class="page-scroll"><a target="_blank" href="http://goo.gl/VCbeOp" class="btn primary-btn">Buy Now<i class="glyphicon glyphicon-chevron-right"></i></a></span>
</div>
</div>
</li>
<li data-transition="slidehorizontal" data-slotamount="5" data-masterspeed="700" data-title="Slide 3">
<img src="img/home/banner-slider/slider-bg.jpg" alt="slidebg" data-bgfit="cover" data-bgposition="center center" data-bgrepeat="no-repeat">
<div class="slider-caption container">
<div class="tp-caption rs-caption-1 sft start"
data-hoffset="0"
data-x="0"
data-y="85"
data-speed="800"
data-start="1500"
data-easing="Back.easeInOut"
data-endspeed="300">
<img src="img/home/banner-slider/shoe3.png" alt="slider-image">
</div>

<div class="tp-caption rs-caption-2 sft "
data-hoffset="0"
data-y="119"
data-x="800"
data-speed="800"
data-start="2000"
data-easing="Back.easeInOut"
data-endspeed="300">
Canvas Sneaker
</div>

<div class="tp-caption rs-caption-3 sft"
data-hoffset="0"
data-y="185"
data-x="800"
data-speed="1000"
data-start="3000"
data-easing="Power4.easeOut"
data-endspeed="300"
data-endeasing="Power1.easeIn"
data-captionhidden="off">
Exclusive to <br>
BigBag <br>
<small>Spring / Summer 2016</small>
</div>

<div class="tp-caption rs-caption-4 sft"
data-hoffset="0"
data-y="320"
data-x="800"
data-speed="800"
data-start="3500"
data-easing="Power4.easeOut"
data-endspeed="300"
data-endeasing="Power1.easeIn"
data-captionhidden="off">
<span class="page-scroll"><a target="_blank" href="http://goo.gl/VCbeOp" class="btn primary-btn">Buy Now<i class="glyphicon glyphicon-chevron-right"></i></a></span>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>




<?php include 'footer.php'; ?>

Footer

File name : footer.php



<script src="<?php echo base_url();?>assets/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="<?php echo base_url();?>assets/plugins/jquery-ui/jquery-ui.js"></script>
<script src="<?php echo base_url();?>assets/plugins/bootstrap/js/bootstrap.min.js"></script>
<script src="<?php echo base_url();?>assets/plugins/rs-plugin/js/jquery.themepunch.tools.min.js"></script>
<script src="<?php echo base_url();?>assets/plugins/rs-plugin/js/jquery.themepunch.revolution.min.js"></script>
<script src="<?php echo base_url();?>assets/plugins/owl-carousel/owl.carousel.js"></script>
<script src="<?php echo base_url();?>assets/plugins/selectbox/jquery.selectbox-0.1.3.min.js"></script>
<script src="<?php echo base_url();?>assets/plugins/countdown/jquery.syotimer.js"></script>
<script src="<?php echo base_url();?>assets/options/optionswitcher.js"></script>
<script src="<?php echo base_url();?>assets/js/custom.js"></script>

</body>
</html>

Header

File name : header.php

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Starhotel</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="shortcut icon" href="favicon.ico">


<base href="<?php echo base_url();?>">


<link rel="stylesheet" href="assets/css/animate.css">
<link rel="stylesheet" href="assets/css/bootstrap.css">
<link rel="stylesheet" href="assets/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/css/owl.carousel.css">
<link rel="stylesheet" href="assets/css/owl.theme.css">
<link rel="stylesheet" href="assets/css/prettyPhoto.css">
<link rel="stylesheet" href="assets/css/smoothness/jquery-ui-1.10.4.custom.min.css">
<link rel="stylesheet" href="assets/rs-plugin/css/settings.css">
<link rel="stylesheet" href="assets/css/theme.css">
<link rel="stylesheet" href="assets/css/colors/turquoise.css" id="switch_style">
<link rel="stylesheet" href="assets/css/responsive.css">
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans:400italic,600italic,400,600,700">

<!-- Javascripts -->
<script type="text/javascript" src="assets/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap-hover-dropdown.min.js"></script>
<script type="text/javascript" src="assets/js/owl.carousel.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.parallax-1.1.3.js"></script>
<script type="text/javascript" src="assets/js/jquery.nicescroll.js"></script>
<script type="text/javascript" src="assets/js/jquery.prettyPhoto.js"></script>
<script type="text/javascript" src="assets/js/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.forms.js"></script>
<script type="text/javascript" src="assets/js/jquery.sticky.js"></script>
<script type="text/javascript" src="assets/js/waypoints.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.isotope.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.gmap.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="assets/rs-plugin/js/jquery.themepunch.tools.min.js"></script>
<script type="text/javascript" src="assets/rs-plugin/js/jquery.themepunch.revolution.min.js"></script>
<script type="text/javascript" src="assets/js/switch.js"></script>
<script type="text/javascript" src="assets/js/custom.js"></script>

How to Load Multiple Views in single View in CodeIgniter?

File name : Controller :- User.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

public function __construct() {

parent::__construct();

// load base_url
$this->load->helper('url');
}

public function index(){

$data['content'] = "Home Page";
// load view
$this->load->view('header_view');
$this->load->view('home_view',$data);
$this->load->view('footer_view');

}

}

You can also load view in another view page.

Header view

File name : header_view.php

<!doctype html>
<html>
<head>
<style>
/* Menu */
.menu {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: darkturquoise;
}

.menu li {
float: left;
}

.menu li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

.menu li ul{
display: none;
}

.menu li a:hover {
background-color: #2b90f5;
}

.active{
background: #2b90f5;
}

footer{
bottom: 0;
position: absolute;
border-top: 1px solid gray;
width: 98%;
padding: 20px 0px;
text-align: center;
}
</style>

</head>
<body>

<ul class='menu'>
<li><a href="<?= site_url() ?>">Home</a></li>
<li><a href="#">Aboutus</a></li>
</ul>

Footer view

File name : footer_view.php

<footer>
Copywrite &copy; <?php echo date('Y'); ?>
</footer>
</body>
</html>

File name :

<?php
// Load header_view
$this->load->view('header_view');
?>
<h2><?= $content; ?></h2>
<table>
<tr>
<td>Name : </td>
<td><input type='text' /></td>
</tr>
<tr>
<td>Username : </td>
<td><input type='text' /></td>
</tr>
<tr>
<td>Email : </td>
<td><input type='text' /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type='button' value='Submit' /></td>
</tr>
</table>
<?php
// Load footer_view
$this->load->view('footer_view');
?>

Syntax for View Files

If you do not utilize CodeIgniter’s template engine, you’ll be using pure PHP in your View files. you use PHPs alternative syntax for control structures and short tag echo statements.


Automatic Short Tag Support

Note : If you find that the syntax described in this page does not work on your server it might be that “short tags” are disabled in your PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly, allowing you to use that syntax even if your server doesn’t support it. This feature can be enabled in your config/config.php file.

Alternative Echos

File name : index.php

Normally to echo, or print out a variable you would do this:

<?php echo $variable; ?>
With the alternative syntax you can instead do it this way:

<?=$variable?>

Alternative Control Structures

File name : index.php

Controls structures, like if, for, foreach, and while can be written in a simplified format as well. Here is an example using foreach:

<ul>

<?php foreach ($todo as $item): ?>

<li><?=$item?></li>

<?php endforeach; ?>

</ul>
Notice that there are no braces. Instead, the end brace is replaced with endforeach. Each of the control structures listed above has a similar closing syntax: endif, endfor, endforeach, and endwhile

Also notice that instead of using a semicolon after each structure (except the last one), there is a colon. This is important!

Here is another example, using if/elseif/else. Notice the colons:

<?php if ($username === 'sana'): ?>

<h3>Hi Sana</h3>

<?php elseif ($username === 'mahi'): ?>

<h3>Hi mahi</h3>

<?php else: ?>

<h3>Hi unknown user</h3>

<?php endif; ?>





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here