Hello guys, I'm making a little "business planner" thingy online. Each user can fill in a template-thingy form and store it.
The thing is, the form has many elements and it's a chore to store (mysql) each manually. Is there some kind of way to store all that chunk of info (textboxes mainly) automagically in the DB ?
Well there's always a sacrifice of manual labour vs being able to index data clearly. For one, if you absolutley have no use of each individual field you can simply create a array of all the fields then serialize it in php before you store it as one huge magic blob in mysql. The downside is that mysql wont be able to tell the difference and you wont be able to query on the data.
The other way is to write a script to automatically map inputs to mysql fields and generate a query based on it. Thats what the MP40 dudes would call semi-automatic mode
Then there's always the dreaded hands-on approach. The good news is that you'll only have to write that code once if you plan it right.
What you want/need is ORM - Object Relational Mapping. Doctrine is probably the most common one in PHP right now, may want to give that a try.
Basically what it means is that you create a PHP object like below, and somewhere it has a map of which field on the object is which field in the DB. You then only have to match each field from the POST data to the appropriate field in the object, which Doctrine has a simple utility method for.
Code:
class Something {
public $id;
public $title;
public $something;
}
$something = new Something();
$something->fromArray($_POST);
$something->save();
The benefit to using something like Doctrine is that you can also do the validation within the object by using setters; so for the title you'd have a method setTitle($value); and within that function you can check whether it meets requirements etcetera
Signature/Avatar nuking: none (can be changed in your profile)
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum