好的,所以通常我这样做不会有问题,而且它会相当直接,但这次不会太多。
下面是我的控制器中的相关代码:
// 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;
}现在,理想情况下,我应该能够访问$product、$category和从products模型返回的任何变量。执行print_r时,我会得到以下结果:
Array ( [category] => 1 [product] => 1 [0] => Array ( [id] => 1 [product_name] => Duff ) )注意foreach语句生成的内容是如何包含在它自己的数组中的。最简单的解决方案是知道如何从视图访问第二个数组,只需传递$this->data即可。
如果这是不可行的,我可以改变什么来在数据数组中分配模型的关联值,而不是在数据数组中创建另一个数组?
该模型只返回get_where语句中的键、值对。
发布于 2012-12-17 06:13:21
在将数据传递给视图之前,应该对数据使用关联数组。尝试更改以下行:
foreach ( $productInformation as $variable => $value )
{
$this->data[$variable] = $value;
}有了这个:
foreach ( $productInformation as $variable => $value )
{
$this->data['product_information'][$variable] = $value;
}然后,在您的视图中,您可以使用$product_information变量访问产品信息。
注意:我假设您使用以下命令将数据传递给视图:
$this->load->view('your_view', $this->data);https://stackoverflow.com/questions/13905942
复制相似问题