0

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

2
  • you code that gets the data from the db, is inside the getAll() method. Why do you expect it to be run when you visit some other route? Please add your home/branch thingy controller method Commented May 4, 2014 at 12:36
  • np. I have added an answer. Commented May 4, 2014 at 12:56

4 Answers 4

0

Like method getall() you should have method branch in same controller.

    public function getAll(){
        $this->load->model('company');
        $data['results'] = $this->company->getCompany();
        $this->load->view('branch', $data);
    } 

    public function branch(){
        $this->load->model('company');
        $data['results'] = $this->company->getCompany();
        $this->load->view('branch', $data);
   }

The error is because you are not passing data['result'] to view.

Sign up to request clarification or add additional context in comments.

1 Comment

i have read the article about how to pass data From Controller To View In CodeIgniter cloudways.com/blog/how-to-pass-data-in-codeigniter
0

Use:

if(isset($results)) 

instead of:

if(!empty($results))

In your view, branch.php

Comments

0

try this

$this->load->view('branch', array('data' => $data));

Comments

0

You can try this alternative way

Change your company model to :

public function getCompany(){

   $q = $this->db->get('company');

    return $q->result_array();
}

Change your view to :

foreach($results as $res) 
  {
    echo "Name: ".$res['company'];
  }

OR

Change your company model to :

public function getCompany(){

   $q = $this->db->get('company');

    return $q->result();
}

Change your view to :

foreach($results as $res) 
  {
    echo "Name: ".$res->company;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.