View
What is Views?

All HTML page we save in view folder, Now i see how to create HTML page and call this page.

Step 1:
create webpage and save in view folder like test.php

<html>
<head>
<title>My Test Page</title>
</head>
<body>
<h1>Welcome to my Test Page</h1>
</body>
</html>


Step 2:
Load page in inside any controller.

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

class User extends CI_Controller {

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

}


Step 3:
Run URL

http://127.0.0.1/classified/index.php/user


How to Send Data From Controller to HTML page

Step 1:
Create Variable in Controller Funtion

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

class User extends CI_Controller {

public function index()
{
$data['title'] = "My Title";
$data['content'] = "My Content";
$this->load->view('mytestpage',$data);
}

}

 

Step 2:
Create HTML page save in view folder like page name is mytestpage.php

<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $content; ?></h1>
</body>
</html>

 

Run URL

http://127.0.0.1/classified/index.php/user

Creating Loops

Create file mytest.php save in Controller folder

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

class User extends CI_Controller {

public function index()
{
$data['user_list'] = array('shishir', 'Rajeev', 'Manoj');
$this->load->view('mytestpage',$data);
}

}

Create file mytestpage.php save in View folder

<html>
<head>
<title>My Page</title>
</head>
<body>
<ul>
<?php foreach ($user_list as $item):?>
<li><?php echo $item;?></li>
<?php endforeach;?>
</ul>
</body>
</html>


Previous
Next