Form data is in an array?
Permalink
Heres my code
When I use this, the data is saved in a double array, so like this
so then I have to do a double foreach, and its also causing issues with the deletion process, since all the areas are the same (Array) Any ideas how to save the data so that it is not in an array? Thanks.
$db = Loader::db(); $sql = "INSERT INTO single_page_areas (areas) VALUES (?)"; $vals = $this->post('areaName'); $db->query($sql, $vals);
When I use this, the data is saved in a double array, so like this
so then I have to do a double foreach, and its also causing issues with the deletion process, since all the areas are the same (Array) Any ideas how to save the data so that it is not in an array? Thanks.
this data comes from a dashboard view.php, and the code is in the controller. It is a table that I made. What I'm trying to do is to have it all in one array so that it is easy to loop through. I am encountering issues when I use because they are all named Array, so I can either delete none or all. Ideally I want to have the data stored like this , so that then I can use
thanks jordan
DELETE FROM single_page_areas WHERE areas="Array ([areas] => $deleteAreaName)"
Array ( [0] => area name 1 [1] => area name 2)
DELETE FROM single_page_areas WHERE areas="deleteAreaName"
thanks jordan
You absolutely do *not* want to store the string representation of an array in your database -- that breaks every rule of data normalization and code modularity in the book!
Step back and think about what arrays are as a concept -- basically an array in php is like an entire record in a database (not just one field). The array "key" is the database table's "column" name, and the array "value" is the value in one row for that one column.
So what you want to do is "convert" the array structure to a database structure when saving. Usually in cases like this you'd have two tables with a foreign key relation between them. But since you don't really have any "data" other than the area names, you can probably get away with just having one table that has 2 id fields in it -- one is the key of the outer array, and the other is the key for the inner array (and the third field is the area name).
When you save the arrays, you want to loop through them and construct multiple SQL statements to save each array element as a different record. Then you can query on that first id field to retrieve all of the area names in one group (or you could delete all areas in that one group as well, based on that first id number).
Does that make sense?
Step back and think about what arrays are as a concept -- basically an array in php is like an entire record in a database (not just one field). The array "key" is the database table's "column" name, and the array "value" is the value in one row for that one column.
So what you want to do is "convert" the array structure to a database structure when saving. Usually in cases like this you'd have two tables with a foreign key relation between them. But since you don't really have any "data" other than the area names, you can probably get away with just having one table that has 2 id fields in it -- one is the key of the outer array, and the other is the key for the inner array (and the third field is the area name).
When you save the arrays, you want to loop through them and construct multiple SQL statements to save each array element as a different record. Then you can query on that first id field to retrieve all of the area names in one group (or you could delete all areas in that one group as well, based on that first id number).
Does that make sense?
sorta- sql and databases aren't my thing. I'll try it and get back to you.
Where does this data come from? Is the single_page_areas a built-in c5 table or one of your own creation? What is the task you're actually trying to achieve here? And how do you *want* the data to be stored (in other words, if the data contains multiple values, don't you want to store it as multiple values? or do you want to pick the first one or combine them somehow?)