将多个变量传递给视图? [英] Passing multiple variables to a view?

查看:66
本文介绍了将多个变量传递给视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有两个变量$posts$comments分别保存帖子和评论数组,我有一个单独的视图来接受这些变量,执行一个foreach循环并将它们打印在同一页面上.这里的问题是,如何将两个变量都传递给视图?

so,i have two variables $posts and $comments that holds the array of posts and comments respectively,i have a separate view that accepts these variables,executes a foreach loop and prints them on the same page. The question here is,how do i pass both the variables to a view?

如果它是单个变量,则像这样$this->load->view('myview',$myvar)传递给它.

If its a single variable i pass it like this $this->load->view('myview',$myvar).

我尝试将其作为这样的数组传递,但仍然无法正常工作.

I tried passing it as an array like this,but still it doesnt work.

$data=array($var1,$var2);
$this->load->view('myview',$data);

任何帮助将不胜感激!谢谢.

Any help would be greatly appreciated! Thanks.

推荐答案

在传递变量时,您需要在其视图中访问该变量.使用array($var1,$var2);是有效的,但可能不是您想要实现的.

You need to access the variable in your view as you pass it. Using array($var1,$var2); is valid but probably not what you wanted to achieve.

尝试

$data = $var1 + $var2;

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

相反.请参阅 视图 ,以获取有关如何访问传递给变量的变量的详细文档.视图.

instead. See Views for detailed documentation how to access variables passed to a view.

使用array($var1,$var2);的问题在于,您将创建两个视图变量:${0}${1}(数组的键成为变量名,数组中有两个键:01 ).

The problem with using array($var1,$var2); is that you will create two view variables: ${0} and ${1} (the array's keys become the variable names, you have two keys in your array: 0 and 1).

这些变量名是无效标签,因此您需要在视图级别用{}括起该名称.

Those variable names are invalid labels so you need to enclose the name with {} on the view level.

由于您失去了有效命名变量的能力.

By that you loose the ability to name the variables usefully.

这篇关于将多个变量传递给视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆