codeigniter - 传递多个值,查看 [英] Codeigniter - passing multiple values to view
问题描述
好了,所以一般我不会有问题,这样做的,这将是相当简单的,但没有这么多这一次。
Okay, so generally I wouldn't have a problem doing this and it would be fairly straight forward, but not so much this time.
下面是相关code在我的控制器:
Here is the relevant code in my controller:
// In order to properly build the form url, we need to include the
// category and product to the view
$this->data = array(
'category' => $category,
'product' => $product
);
// Load the product model and get editable values from the database
$this->load->model('productadmin/products_model');
$productInformation = $this->products_model->get_product_data($product);
// Modular code! Use variable-variables to prevent having to write multiple lines of code
// when we start returning more information from the data
foreach ( $productInformation as $variable => $value )
{
$this->data[$variable] = $value;
}
现在,理想情况下,我应该能够访问$产物,$类别和从产品模型返回的任何变量。做一个的print_r,我得到如下:
Now, ideally, I should be able to access $product, $category and any variables returned from the products model. Doing a print_r, I get the following:
Array ( [category] => 1 [product] => 1 [0] => Array ( [id] => 1 [product_name] => Duff ) )
注意怎么样是由foreach语句生成包含在它自己的阵列。最简单的解决方案,将是知道如何从视图访问第二个数组,只是路过 $这个 - >数据
。
如果这不是做的,能有什么我可以改变这种状况会不会造成它在另一个数组分配模型的数据数组中的关联值?
If that's not do-able, what can I change that would assign the model's associative values inside the data array without creating another array inside of it?
该模型简单地返回从get_where声明键,值对。
The model simply returns key, value pairs from a get_where statement.
推荐答案
您应该使用关联数组为您的数据之前,它被传递给视图。尝试改变这一行:
You should use an associative array for your data before it is passed to the view. Try changing this lines:
foreach ( $productInformation as $variable => $value )
{
$this->data[$variable] = $value;
}
这一点:
foreach ( $productInformation as $variable => $value )
{
$this->data['product_information'][$variable] = $value;
}
然后在您的视图,您可以访问使用你的产品信息 $ PRODUCT_INFORMATION
变量。
注:我假设你正在使用传递数据的视图:
Note: I am assuming that you're passing your data to the view using:
$this->load->view('your_view', $this->data);
这篇关于codeigniter - 传递多个值,查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!