首页 文章

Codeigniter - 传递多个值进行查看

提问于
浏览
2

好的,所以一般来说,我不会有这样做的问题,这将是相当直接的,但这次不是那么多 .

这是我的控制器中的相关代码:

// 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以及从产品模型返回的任何变量 . 做一个print_r,我得到以下内容:

Array ( [category] => 1 [product] => 1 [0] => Array ( [id] => 1 [product_name] => Duff ) )

注意foreach语句生成的内容是如何包含在它自己的数组中的 . 最简单的解决方案是知道如何从视图中访问第二个数组,只需传递 $this->data 即可 .

如果那不可行,我可以更改哪些内容将在数据数组中分配模型的关联值而不在其中创建另一个数组?

该模型只返回get_where语句中的键值对 .

1 回答

  • 1

    在将数据传递给视图之前,应该为数据使用关联数组 . 尝试更改此行:

    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);
    

相关问题