douwu5009 2012-12-07 23:06
浏览 18
已采纳

如何访问codeigniter中的数组字段?

I'm trying to pass two arrays ($a_1 and $a_2) from my controller to my view like so:

$this->load->view('confirm_data_v', $a_1, $a_2);

In my view I want to print the value of one of them doing this:

<p><?php echo $name ?></p>
<p><?php echo $mail ?></p>

when I print each array I get this:

Array
(
    [name] => jon
)
Array
(
    [mail] => blabla@server.com

)

$name is a field inside $a_1 and $mail is a field inside $a_2, but it seems like the view doesn't know where these fields are, I mean, it doesn't know in wich array is $name and $mail, wether $a_1 or $a_2. How do I do that?.

  • 写回答

2条回答 默认 最新

  • dongshu4221 2012-12-07 23:12
    关注

    You're passing the arrays in an incorrect way. You can only pass one data array as a second parameter while loading the view.

    You could instead put each array in the data array in your controller:

    $data['a_1'] = $a_1;
    $data['a_2'] = $a_2;
    $this->load->view('confirm_data_v', $data);
    

    Then in your view you can access $a_1 and $a_2 as you like

    Name: <?php echo $a_1['name']; ?>
    Email: <?php echo $a_2['mail']; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?