The problem I am having is that my ecommerce script allows people to add products to their store. However, those products can have variants… like Color and those variants can have options, like Black, White, and Blue for example. The problem I am having is how exactly do I determine how many options they have entered. Eventually I will have a javascript function add a new input field if they click “add option” but for now I just made 3 input fields like so:
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>
Now what I don’t know how to do is loop through all the variant_options and if they are not empty add them to a database. I don’t want to hard code it in with the names because I don’t know how many options they will choose per variant.
I tried doing this:
$i = 1;
while($this->input->post('variant_option_$i') != '')
{
$variant_options[] = $this->post->input('variant_option_$i');
$i++;
}
But that does not seem to work, as the variant title gets added to the database, however the options do not.
The only thing I need a solution to is to how to actually grab those fields all at once. I was thinking a foreach loop but im not sure.
Thank you.
I would use an array of input fields instead:
Then check for the values like: