Codeigniter 2.2.2 /如何在从控制器传入的“视图”中打印数组数据?

时间:2022-11-29 21:36:56

I am not receiving any of the "country" data that I am passing from my controller to my view. Here is my controller:

我没有收到任何我从控制器传递给我的视图的“国家/地区”数据。这是我的控制器:

    public function country_destination()
{

    $this->load->model('model_admin');
    //Calling the get_unique_states() function to get the arr of state. Model already loaded.
    $arrCountries = $this->model_admin->get_unique_countries();

    //Getting the final array in the form which I will be using for the form helper to create a dropdown.
    foreach ($arrCountries as $countries) {
        $arrFinal[$countries->country] = $countries->country;
    }
    $data['countries'] = $arrFinal;

            // Basis page data
            $data = array(
                'templateVersion'   => 'template1',
                'headerVersion'     => 'header1',
                'css'               => '<link rel="stylesheet" href="/css/compiled/index.css" type="text/css" media="screen" />
                                       <link rel="stylesheet" type="text/css" href="/css/lib/animate.css" media="screen, projection" />',
                'navBarVersion'     => 'navbar1',
                'main_content'      => 'detail-wrap/admin/country_destination', 
                'page_title'        => 'example.com - App',
                'footerVersion'     => 'footer1'
                );
            //echo "here";
            $this->load->view('detail-wrap/includes/template1', $data);      
}

In my view, I would like to dump the contents of $data which includes $data[countries]. This will help me determine if indeed 'some' data is being passed and aid with further debugging.

在我看来,我想转储包含$ data [countries]的$ data的内容。这将帮助我确定是否确实传递了“某些”数据并帮助进一步调试。

I have tried print_r($data); but I get an 'undefined variable: data' error.

我试过print_r($ data);但我得到一个'未定义的变量:数据'错误。

2 个解决方案

#1


NOTE: the $data array's keys are converted into variables

注意:$ data数组的键被转换为变量

You have to do

你必须做

print_r($countries);  //to print the countries

in your view

在你看来

You have to give some key to this array

你必须给这个数组一些关键

 $data['somekey'] = array(
                'templateVersion'   => 'template1',
                'headerVersion'     => 'header1',
                'css'               => '<link rel="stylesheet" href="/css/compiled/index.css" type="text/css" media="screen" />
                                       <link rel="stylesheet" type="text/css" href="/css/lib/animate.css" media="screen, projection" />',
                'navBarVersion'     => 'navbar1',
                'main_content'      => 'detail-wrap/admin/country_destination', 
                'page_title'        => 'example.com - App',
                'footerVersion'     => 'footer1'
                );

And access this data in the view with the help of the key. CI send the key as a normal php variable to the view

并在密钥的帮助下在视图中访问此数据。 CI将密钥作为普通的php变量发送到视图

So from view you can access as print_r($somekey)

所以从视图中你可以访问print_r($ somekey)

#2


In your view file you can write like this to show the array data in structured format.

在您的视图文件中,您可以像这样编写以结构化格式显示数组数据。

echo "<pre>";

print_r($countries);

#1


NOTE: the $data array's keys are converted into variables

注意:$ data数组的键被转换为变量

You have to do

你必须做

print_r($countries);  //to print the countries

in your view

在你看来

You have to give some key to this array

你必须给这个数组一些关键

 $data['somekey'] = array(
                'templateVersion'   => 'template1',
                'headerVersion'     => 'header1',
                'css'               => '<link rel="stylesheet" href="/css/compiled/index.css" type="text/css" media="screen" />
                                       <link rel="stylesheet" type="text/css" href="/css/lib/animate.css" media="screen, projection" />',
                'navBarVersion'     => 'navbar1',
                'main_content'      => 'detail-wrap/admin/country_destination', 
                'page_title'        => 'example.com - App',
                'footerVersion'     => 'footer1'
                );

And access this data in the view with the help of the key. CI send the key as a normal php variable to the view

并在密钥的帮助下在视图中访问此数据。 CI将密钥作为普通的php变量发送到视图

So from view you can access as print_r($somekey)

所以从视图中你可以访问print_r($ somekey)

#2


In your view file you can write like this to show the array data in structured format.

在您的视图文件中,您可以像这样编写以结构化格式显示数组数据。

echo "<pre>";

print_r($countries);