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 ステートメントによって生成されたものが独自の配列に含まれていることに注意してください。最も簡単な解決策は、 を渡すだけで、ビューからその 2 番目の配列にアクセスする方法を知ること$this->dataです。

それができない場合、データ配列内に別の配列を作成せずに、データ配列内にモデルの連想値を割り当てるには、何を変更できますか?

このモデルは、get_where ステートメントからキーと値のペアを返すだけです。

4

1 に答える 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);
于 2012-12-16T22:13:20.823 に答える