HTML form generation from the database and store value into the database

I have a “t_form” table and a “t_attribute” table. It looks a little bit like below.

Form table

form_id | form_name    | description
-----------------------------------
1       | Laptop  	   | Possible attributes are Model, Screen Size, OS, Cd Player
2       | Mobile  	   | Possible attributes are Model, OS
3       | Tablet  	   | Possible attributes are Model, Screen size, OS

Attribute table

attribute_id | attribute
------------------------
1  		     | Model
2       	 | Screen Size
3       	 | OS
4       	 | Cd Player
  1. Possible attributes of type Laptop form are Model, Screen Size, OS, Cd Player.
  2. Possible attributes of type Mobile form are Model, OS.
  3. Possible attributes of type Tablet form are Model, Screen size, OS.

Form_Attribute table

form_id | attribute_id
----------------------
1       | 1 
1       | 2
1       | 3
1       | 4
2       | 2
2       | 2
3       | 2
3       | 2
3       | 2

I will generate the HTML form like below(with form fields) according to the selection of form type(either Laptop, Mobile or Tablet) by the user from the database.

For Laptop:

enter image description here

For Mobile:

enter image description here

For Tablet:

enter image description here

Questions:
Here are my questions.

  1. What is better approach to store the data entered by the user into the database.
  2. What is better approach to retrieve the data from the database, because I want to achieve the filter option, for example: I want to fetch the result which laptop screen size is avilable from 14 to 16.

Filter:

enter image description here

Please provide a feasible solution with some example for better understanding to me and help me to achieve this.

What about a single table with the following fields:

type | Model | Screen Size | OS | Cd Player

type specified whether it is a mobile, laptop or tablet.

Some fields (e.g. Cd Player) for some items (e.g. Mobile) are not used. But the processing/query on this single table is unified and is much easier.

This, of course, has the cost of some additional storage space in the DB.

This may not be the best solution. it should be feasible as far as I can tell.


Thanks for your reply, but this is a sample list of category, but in real time I have a bunch of categories say 40 to 45, each category .i.e each form hold many fields say 15 to 18. Your approach consume very much of storage in the database. Yes, I agree you this approach query processing would be better but I want a better approach interms of less storage and optimized table design. Thanks.


I see.

Then, you original design looks good for this purpose. The data may be stored in one or several tables like:

item_id | attribute_id | attribute

If the attribute has several types, you may need several tables for different attribute types (to save space). If there is only several attribute types, you may consider a similar solution as in this answer.

For querying, you need to join several tables.


Can you please tell me with some sample data, I am not getting clear.
Tell me for entry for the form type laptop, I want to store the data (Possible attributes of type Laptop form are Model, Screen Size, OS, Cd Player) in the database. How to store this in the table as per the second approach?


Okay, one example:

items table:

item_id | form_id (maybe type_id, which is better)
------------------------------------------------------------
9       |  1 (for laptop as in your question)

attributes table:

item_id | attribute_id | attribute
----------------------------------------------
9       |  1           |  "HP model"
9       |  2           |  "14"
9       |  3           |  "Linux"
9       |  3           |  "Golden CD Player"

You may use another attribute table to store 9 | 2 | "14" where 14 is an integer.

Leave a Reply

Your email address will not be published. Required fields are marked *