"mycontroller.php"
. Write the following code:
[php]
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Mycontroller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
function index() //index is the default method
{
$this->load->view('myview');//Loading view page namely myview.php
}
}
?>
[/php]
"myview.php"
. Write the following code:
[php]
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<html>
<body>
<div id="container">
<h1>Hello!</h1>
<div id="body">
<p>Welcome to Splessons</p>
<img class="image_center" src="<?php echo base_url('images/download.png')?>"/>
</div>
</div>
</body>
</html>
[/php]
mymodel.php
under application/models folder.
[php]
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Mymodel extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
//Retrieve name from database function get_names()
{
$this->db->select('name');
$this->db->from('names');
$res=$this->db->get()->row_array();
return $res;
}
}
[/php]
Then replace index()
method in mycontroller with the below code and check for output.
[php]
function index() //index is the default method
{
$this->load->model('mymodel');//Load model
$data['names']=$this->mymodel->get_names();//get names from database and store in a variable
$this->load->view('myview',$data);//Passing Data to view and Loading view page namely myview.php
}
[/php]
Eg
: MYCONTROLLER(Upper cased), MyController(Camel Cased), my-controller(Hyphened), my controller(spaced).my_controller
(If needed to join multiple names) instead which might leads to errors when moved to live because local servers use windows OS(with Case insensitive feature) but live servers use Linux OS(Case sensitive feature).
Class My_controller extends CI_controller
unless codeigniter is of latest version.