简体   繁体   中英

Undefined variable in CodeIgniter view 5

I am learning CodeIgniter and I got stuck on this error. $categories is fine, but when I want to see $nationality I have an error

Message: Undefined variable: nationality

Filename: pages/add_recipe.php

add_recipes.php

<?print_r($categories);?>
<?print_r($nationality);?>

My controller

class Pages extends CI_Controller
    {    
    public function add_recipe($page = 'add_recipe')
            {
                $this->load->helper('url');

                if (!file_exists('application/views/pages/' . $page . '.php')) {
                    show_404();
                }
                $this->load->model('Categories_model');
                $data['categories'] = $this->Categories_model->get_categories();

                $this->load->model('Nationality_model');
                $data['nationality'] = $this->Nationality_model->get_nationality();

                $this->load->view('templates/header');
                $this->load->view('templates/nav');
                $this->load->view('pages/' . $page, $data);
                $this->load->view('templates/sidebar', $data);
                $this->load->view('templates/footer');
            }
    }

Models

public function get_nationality($conditions=null,$limit=null,$offset=0,$order=null){
        if($conditions!=null){
            $this->db->where($conditions);
        }
        if($order!=null) {
            $this->db->order_by($order);
        }
        $query = $this->db->get('nationality',$limit,$offset);
        return $query->result();
    }

public function get_categories($conditions=null,$limit=null,$offset=0,$order=null){
        if($conditions!=null){
            $this->db->where($conditions);
        }
        if($order!=null) {
            $this->db->order_by($order);
        }
        $query = $this->db->get('categories',$limit,$offset);

        foreach($query->result_array() as $row ){
            if(!$row['parent_id']){ 
                $data[$row['id']][] = $row['title']; 
            }else {
                $data[$row['parent_id']]['sub'][$row['id']] = $row['title']; 
            }
        }
        return $data;
    }
Can you please update the Nationality model function get_nationality with this, try it...

public function get_nationality($conditions=null,$limit=null,$offset=0,$order=null){
        if($conditions!=null){
            $this->db->where($conditions);
        }
        if($order!=null) {
            $this->db->order_by($order);
        }
        $query = $this->db->get('nationality',$limit,$offset);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }
            return $data;
        }
        return false;
    }

原来,该页面是从另一个控制器调用的,与名称混淆

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM