View
What is Views?

All HTML page we save in application->view

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

Here we create array in index function of User controller

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

Passing Data from Controller to View by Model

Controller

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

class User extends CI_Controller {

public function index()
{
$this->load->model('user_model');
$data['users']=$this->user_model->get_users();
$this->load->view('user_list',$data);
}
}

Model

<?php
class User_model extends CI_Model{

public function get_users(){

return [
['name'=>'shishir sharma','email'=>'shishirdeveloper@gmail.com'],
['name'=>'som kumar','email'=>'somdeveloper@gmail.com'],
['name'=>'honey sharma','email'=>'honeydeveloper@gmail.com']
];

}

}
?>

View

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html>
<head>
<title>View with Data</title>
</head>
<body>
<?php
foreach($users as $user){
echo $user['name']." ".$user['email'];
echo "<br>";
}
?>
</body>
</html>


Previous
Next