At first connect the database in your project file and connect the model page in your view page and try this code.
View Page:
<?php $get_data=$this->MyModel->get_table_data(); ?> <table class="table table-bordered"> <thead> <tr> <th> Name </th> <th> Email </th> <th> Phone No </th> </tr> </thead> <tbody> <?php foreach($get_data as $val){ ?> <tr> <td><?php echo $val->name; ?></td> <td><?php echo $val->email; ?></td> <td><?php echo $val->phone; ?></td> </tr> <?php } ?> </tbody> </table>
Model Page:
public function get_table_data(){ $qry=$this->db->select('*')->from('Table Name')->get()->result(); return $qry; }
You can direct access Model page from View page in Codeigniter.
First, you need to check if you model class name have in parent::__construct(). If you can’t define Model class name then do it first. For example:
public function __construct(){
parent::__construct();
$this->load->helper(‘url’);
$this->load->helper(‘form’);
// Model
$this->load->model(‘Model Classs Name Here’);
}
Now you can access model from you view page. Like
<?php
$this->MyModel->SearchResult(); // MyModel is the Model class name and SearchResult is user define function name.
?>
If you want to pass some value from your view page, then follow this code.
<?php
$id=$val->user_id;
$this->MyModel->GetUser($id);
?>
I hope it will be helpful for you.