简体   繁体   English

发送多个数组在codeigniter查看

[英]Send multiple array to view in codeigniter

I have two arrays, first is an array with its data is declared on controller and the second is an array from a query operation.我有两个 arrays,第一个是一个数组,其数据在 controller 上声明,第二个是来自查询操作的数组。 Both of them are not related to other.两者都与其他无关。 Lets name them $array1 and $array2.让我们将它们命名为 $array1 和 $array2。 Below is my controller下面是我的controller

$array1['status'] = 'admin';
$array1['name'] = 'John';

$this->load->model('mymodel');
$array2 = $this->mymodel->get_all_row();

$this->load->view('my_view', $array1, $array2);

Lets say the $array2 have 3 indexes id , name , price .假设$array2有 3 个索引idnameprice

Need some helps.需要一些帮助。

You need to access the variable in your view as you pass it.您需要在传递变量时访问视图中的变量。 Using array($var1,$var2);使用数组($var1,$var2); is valid but probably not what you wanted to achieve.是有效的,但可能不是您想要实现的目标。 See Views for detailed documentation how to access variables passed to a view.请参阅查看详细的文档如何传递给视图访问的变量。

Try尝试

$data = $var1 + $var2;

or或者

$data = array_merge($var1, $var2);

The view accepts only two parameters.视图只接受两个参数。

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method.数据通过视图加载方法的第二个参数中的数组或对象从控制器传递到视图。 Here is an example using an array:下面是一个使用数组的例子:

$data = array(
        'title' => 'My Title',
        'heading' => 'My Heading',
        'message' => 'My Message'
);

$this->load->view('blogview', $data);

Third Parameter of View视图的第三个参数

There is a third optional parameter lets you change the behavior of the method so that it returns data as a string rather than sending it to your browser.第三个可选参数可让您更改方法的行为,使其以字符串形式返回数据,而不是将其发送到浏览器。 This can be useful if you want to process the data in some way.如果您想以某种方式处理数据,这会很有用。 If you set the parameter to TRUE (boolean) it will return data.如果您将参数设置为 TRUE(布尔值),它将返回数据。 The default behavior is false, which sends it to your browser.默认行为是 false,它将它发送到您的浏览器。 Remember to assign it to a variable if you want the data returned:如果要返回数据,请记住将其分配给变量:

$string = $this->load->view('myfile', '', TRUE);

So, bottom line is that you have to send one array.所以,底线是你必须发送一个数组。 Now if you wanna pass data of two array, you should merge these arrays into and then pass merged array.现在如果你想传递两个数组的数据,你应该将这些数组合并到然后传递合并的数组。

$merged_array = array_merge($array1, $array2);
$this->load->view('my_view', $merged_array);

When you want to pass two arrays to your view, you need to convert the data array to a two-dimensional array.当你想将两个 arrays 传递给你的视图时,你需要将数据数组转换为二维数组。 For example, you have two arrays with the following names and contents:例如,您有两个 arrays,其名称和内容如下:

[A1] => Array
    (
        [ID] => '1'
        [Name] => 'Jon'
    )

[A2] => Array
    (
        [Tell] => '123456'
        [Address] => 'Somware'
    )

To pass two arrays, place them in the data array with different keys要传递两个 arrays,将它们放在具有不同键的数据数组中

$data['A1']=$A1;
$data['A2']=$A2;

Result:结果:

[data] => Array
(
    [A1] => Array
        (
            [ID] => '1'
            [Name] => 'Jon'
        )

    [A2] => Array
        (
            [Tell] => '123456'
            [Address] => 'Somware'
        )
)

and use in view like this:并像这样在视图中使用:

echo $A1['ID'];
echo $A1['Name'];
echo $A2['Tell'];
echo $A2['Address'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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