0

I am trying to pass data to view but it gives me that error: Undefined variable: data

Controller:

public function article($page=1)
{           
    $info = array(
        "module_name" => $this->module_name,
        "view_name" => "cms/cms_view", // your view path
        "error_msg" => "",  
        "error_type" => "", 
        "data" => array(
        'content'=>$this->cms_model->get_cms($page),
        'test'=>'Hello world'

        ), //data which will be sent to view
    );
    $this->loadview->view($info);
}

Model:

 public function get_cms($page)
    {
        $query = $this->db->get_where('article', array('art_id' => $page),1);
        return $query->row();
    }

View:

<?php
        echo $data->content;
        ?>
Yusubov
  • 5,815
  • 9
  • 32
  • 69
iman hamaad
  • 7
  • 1
  • 5
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – tereško Jan 19 '13 at 22:59

2 Answers2

2

Note the last line.

Controller:

public function article($page=1) {

    $info = array(
        "module_name" => $this->module_name,
        "view_name" => "cms/cms_view", // your view path
        "error_msg" => "",  
        "error_type" => "", 
        "data" => array(
            'content'=>$this->cms_model->get_cms($page),
            'test'=>'Hello world'

        ), //data which will be sent to view
    );
    $this->load->view('view_name', $info);
}

More info about how to load a view here.

Shomz
  • 37,421
  • 4
  • 57
  • 85
  • Thanks but this didn't solve the problem. and if info isn't seen by the cms_view then why when I try to echo test in the view, it's displayed correctly :S. – iman hamaad Jan 08 '13 at 23:04
  • $info isn't visible, but $module_name, $view_name, etc. $data['content'] and $data['test'] should be visible. – Shomz Jan 08 '13 at 23:27
  • i have read the article about how to pass data From Controller To View In CodeIgniter https://www.cloudways.com/blog/how-to-pass-data-in-codeigniter/ – Owais Alam Aug 28 '17 at 14:59
0

I think the problem may be in your view. The $data variable is setup as an array in your controller but you are treating it like an object. Try using

<?php
     echo $data["content"];
?>

in the view instead and see if that corrects your issue.

Derekc
  • 61
  • 1
  • 9