I been searching this issue all over the internet but I can't seem to find a good answer. I'm using codeigniter framework Version 2.1.4.
I have model: company.php and tbl_name: company
<?php
class Company extends CI_Model
{
public function __construct()
{
$this->load->database();
}
public function getCompany(){
$q = $this->db->get('company');
if($q->num_rows() > 0)
{
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
}
I have controller: home.php where I can call up the model I created.
public function getAll(){
$this->load->model('company');
$data['results'] = $this->company->getCompany();
$this->load->view('branch', $data);
}
lastly I have view: branch.php where I can view data from DB
<?php
if(!empty($results))
{
foreach($results as $res)
{
echo "Name: ".$res->company;
}
}
?>
Now this is the problem: When I browse controller localhost/app/home/getAll, It works fine because I can see the data from database example: Name: ABC Company, but when I browse the views localhost/app/home/branch I get this error below.
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: results
Filename: views/branch.php
Line Number: 11
1
Can you please help me? thanks