0

私が抱えている問題は、私のeコマーススクリプトを使用すると、ユーザーがストアに商品を追加できるようになることです。ただし、これらの製品には、カラーなどのバリエーションがあり、これらのバリエーションには、たとえば、黒、白、青などのオプションがあります。私が抱えている問題は、彼らが入力したオプションの数をどのように正確に判断するかです。最終的には、「オプションの追加」をクリックすると、javascript関数に新しい入力フィールドを追加させることになりますが、今のところ、次のように3つの入力フィールドを作成しました。

Variant Title:<input type="text" name="variant_title"><br>
Variant Option: <input type="text" name="variant_option_1"><br>
Variant Option: <input type="text" name="variant_option_2"><br>
Variant Option: <input type="text" name="variant_option_3"><br>

今、私が行う方法がわからないのは、すべてのvariant_optionsをループし、それらが空でない場合はデータベースに追加することです。バリアントごとにいくつのオプションを選択するかわからないため、名前をハードコーディングしたくありません。

私はこれをやってみました:

$i = 1;
            while($this->input->post('variant_option_$i') != '')
            {
                $variant_options[] = $this->post->input('variant_option_$i');
                $i++;
            }

ただし、バリアントタイトルがデータベースに追加されるため、これは機能しないようですが、オプションは機能しません。

私が解決する必要がある唯一のことは、実際にそれらのフィールドを一度に取得する方法です。foreachループを考えていましたが、よくわかりません。

ありがとうございました。

4

1 に答える 1

2

代わりに、入力フィールドの配列を使用します。

Variant Title:<input type="text" name="variant_title"><br>
Variant Option: <input type="text" name="variant_option[]"><br>
Variant Option: <input type="text" name="variant_option[]"><br>
Variant Option: <input type="text" name="variant_option[]"><br>

次に、次のような値を確認します。

if (is_array($this->input->post('variant_option')):
   foreach ($this->input->post('variant_option') as $value):
       $variant_options[] = $value; // loop through...
   endforeach;
endif;
于 2012-10-04T19:53:33.683 に答える