Let’s start telling that I’m passing an x amount of variables via post from a form.
Let’s name them menu_category_1, menu_category_2, …, menu_category_x, plus, maybe, menu_category_new (I’m using an if empty to check this last one variable).
To make things easier I’m also sending the parameter $key (amount of variables starting from 0).
Now I need to set them into a new variable $menu_category (array), which is going to be imploded and then update my database.
How do I set up that new $menu_category variable to be an array containing all my variables named in the beginning?
I was thinking of using a for loop but I can’t come up with something useful.
Thanks!!!
I ended up going the Quentin way, just adding [] to the keys. I chose this just because it worked right away. However I’m going to dig deeper on janenz00’s sugestion, which I personally think is the way to go.
Thanks to all of you who took the time to go through this!!!!
You’re using PHP, so let us name them
menu_category[0],menu_category[1], etc (ormenu_category[],menu_category[], etc) instead.Then
$_POST['menu_category']or$_GET['menu_category']will be an array.Normalise your database. Express your one-to-many relationships using a foreign key and a second table, not by storing CSV data.