I'm having some trouble deciding which approach I should take. I want to allow users to create their own html forms by choosing different form elements (textfields, textareas, lists, ratios, etc) I guess something similar to http://wufoo.com/ but much more basic. Should I use database tables or create files?
For tables I was thinking to create a table for each form element, e.g
TEXTFIELD TABLE
ID
TEXTFIELD_NAME
USER
...
TEXTAREA TABLE
ID
TEXTAREA_NAME
USER
etc for all form elements
and then just query all of them with..
WHERE user=$user
Or should I generate php files on the server for each form created?
One approach I used for custom forms on a mobile device was to define a table for field types (eg: text, date, money, custom), a table for form templates (with owner, version, and XML for the template data), and a table for form values (as XML) with a foreign key to the template.
Someone may create a form template with the available field types, and the collected data may be stored for that template, and optionally by template version. If the database supports it, you could query into the XML data to aggregate fields across forms.
For presentation, you could use a fixed style, or transform the template or collected fields however desired.
Should I use database tables or create files?
Storing this stuff in a database has the big advantage that you can easily edit them programmatically, and/or populate the user interface used to edit them.
A generated HTML structure you would have to parse first.
So definitely a database.
Related
I'm using mysql DB, I've got table "posts" with columns id, title, text, author_id, image at the moment.
I need to provide a possibility to upload several images to one post in my blog. What's the best way of organizing my DB structure in this case and how it's usually done in Yii 2?
At the moment I just have functionality for saving 1 image and keeping it's path in table field.
Should I keep an array in DB or create another relations table?
When you're working with a conventional RDBMS like MySQL:
It seems you're going from a one-to-one, to a zero- or one-to-many relation, in which case i'd recommend creating another table for your files (for example: image*), containing a foreign key image.post_id to posts.id. Added benefit is that you will be able to more neatly store some metadata about the image, instead of creating a load of extra (but perhaps unneeded columns) in the posts table.
The cleanest solution (imho) is usually to stay close to the data structure of your DMBS, instead of placing arbitrary data structures inside a text field, no matter what framework or language you use.
This is different when working with no-sql database like for example MongoDB, where depending on the use-case you may want to use an array property images on your posts document containing image objects.
*yii naming convention for tables is singular instead of plural
I have a large php form with dynamic fields and there can be additional fields added after releasing into production. How do I store this large data into mysql database using php so that it can handle dynamic fields and additional fields. Example of a form can be seen in the link below.
http://form.jotform.me/form/50655815937465
The above link doesn't contain dynamic fields for now but it may contain later.
The main form is huge so we divided into different sections. Need to handle this as well. Also need a save draft concept where the user can save the data and load it later when he gets a chance to complete it.
There's a lot of different ways to handle this, but I'll make a simple recommendation
For the static fields
Create a column in your table for each static field
Save each static field submission in the corresponding column
For dynamic fields
Create an array of dynamic values and call serialize on it
Store the serialized value in a single long text column in the database
To read the values, you will have to call unserialize on it
Pros
Easy to implement
Ability to filter data based on static inputs
Only uses one extra column for dynamic inputs
Supports a large amount of dynamic form inputs
Cons
Difficult to filter data based on dynamic inputs
Alternative solutions
These solutions are significantly more robust but require a much deeper understanding. I'll leave these here for you to research on your own.
Entity Attribute Value (EAV) model
Document-oriented database
I am looking into creating something similar to a form template system on my web site.
As an example, say I want the users to be able to create form templates (similar to Wufoo, they can define any number of inputs, etc). Then from these created templates, anybody would be able to use these templates, fill them out, and therefore create a number of form instances (this would be possible for each user-defined template). Also, there would be no limit to the number of templates and instances created.
Purely from a server/persistence perspective, what is the best way of creating a system like this? Would I need to create a new database table for each created template and then insert the form instances as records into the table? How well would this scale?
This topic is very broad. As to the scalability, Wufoo (and other form creation websites) currently work. So a brief answer is, it's already being done, so scaleability shouldn't be an issue. However, random generation of multiple tables will get out of control very quickly.
If you are not at a point where you are having this problem, I would first build the system with single tables and UUIDs for the Primary Keys. This makes them moveable later on. Then, if table sizes become an issue, you can split out the tables anyway you see fit. For example, you can have all of the customes whos last name start with A in the a_forms table.
As for the structure of the tables, you would build these as a ONE-TO-MANY. One form can have many elements. The elements can all be predefined (i.e. text, text area, radio button, check box, submit button, etc.) When someone builds a form, you can serialize() the form elements and save them in the table. When the form needs to be rendered, you unserialize(), parse the elements and build the form.
I'm building a site and need to allow users to be able to create custom forms. The part I'm having trouble with is how to store the data from these forms. Since they are user created fields, there will be no corresponding field in the database. What would be the best way of going about this? I assume modifying the table is the incorrect way of doing so. Would putting all the data into an array, serializing it, and then storing it in a field be a good way?
Would putting all the data into an array, serializing it, and then storing it in a field be a good way?
While that would store the data, it would not make the individual elements queryable. It's an astoundingly bad long-term idea.
Take a look at the entity-attribute-value model. You should be able to represent the fields in a form and even the data filled in to the form using EAV backed up by some rigid foreign key checks.
Now, EAV isn't perfect. It can be tough to create certain queries against it because of MySQL's limitations, but that's nothing a bit of external code can't fix. It'll still be ten times better than serialized data.
maybe you can serialize the values of the complete form and storing it in a blob to the database.
Maybe you can provide a solid Form builder framework (or modify an existing solution) where every user created field controlled by you like this:
//input elements named by a custom namespace:
`<input type="text" name="mycustomelement[user_defined_name]" />`
and there you go: when user submits the form, you can catch all 'mycustomelement' element then you can serialize it to store in a db.
I would store a table userforms {id, user, index, fieldname, fieldtype} and then userformresponses {id, userformid, fieldname, response}
An online application we are building (php & mysql) requires users to be able to create their own forms for data capture and record this data in a database, respecting the existing ORM's.
If the forms where "hard coded" then we would simply set the db tables up to store the normalised data ourselves however as our users define the form fields contained in the forms, we're not sure what is the best way to proceed to implement this functionality.
Do we need to think about some kind of meta data or data abstraction layer for our DB? Google hasn't been too much help as we're unsure about how we need to go about this.
Any pointers in the right direction would be gratefully appreciated!
Many content management systems address this problem in different ways.
For example, in Drupal, users can create their own custom content (with custom forms) through the CCK module. The module defines different types of fields that the user can create, then generates tables with specific data types to store the data.
Some tips:
Define your field types - Think about giving the users a choice of different field types (e.g., select box, string, radio).
Create tables for user defined fields - Each field type will have a specific SQL data type. Define a table using these data types. For example, a select box might be mapped to an enum and a input text element might be mapped to a varchar column.
Add data to the new tables - use the new tables to store the data in a somewhat normalized way.
Obviously there are many different approaches, but these are just a few suggestions.
I think I've found a solution to my problem, so for all those people who come along a similar problem have a look at the following artcles -
http://www.adaniels.nl/articles/an-alternative-way-of-eav-modeling/
http://weblogs.sqlteam.com/davidm/articles/12117.aspx
Hope this helps.