0
votes

I couldn't fix following errors

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: results

Filename: views/view_nav.php

Line Number: 22

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: views/view_nav.php

Line Number: 22

here is my code

controller

public function home(){
        $this->load->model('get_company_model');
        $this->load->view('view_header');
        $data['results'] = $this->get_company_model->get_all();

        $this->load->view('view_nav',$data);
        $this->load->view('view_content');
        $this->load->view('view_footer');
    }

model

 public function get_All(){
        $query = $this->db->query("SELECT name,id from companydetails");
        return $query->result();

    }
    public function get_branch($companyID){
        $query = $this->db->query("SELECT name,id from branches WHERE companyid='".$companyID."'");
        return $query->result();

    }

view

 <?php
    foreach($results as $row):
    ?>
    <div>
        <ol class="tree">
    <li>
        <label for="folder1"><a href="<?php echo site_url('site2/about'); ?>"><?=$row->name?></label></a> <input type="checkbox"  id="folder1" /> 
        <ol>
              <?php
                $myresult=$this->get_company_model->get_branch($row->id);
                foreach($myresult as $row2):
                  ?>  
            <li class="file"><a href="#"><?=$row2->name?></a></li>
            <?php
                 endforeach;
                ?>
1

1 Answers

1
votes

You have defined your function incorrectly in your Controller

   $data['results'] = $this->get_company_model->get_all();
                                                   ^It should be:

    $data['results'] = $this->get_company_model->get_All();

Note the Capital A

OR

Try echoing the Query in Model to check either your Query is returning any rows or not.

$query = $this->db->query("SELECT name,id from companydetails");
echo $this->db->last_query();
return $query->result();