5 Replies - 3295 Views - Last Post: 02 May 2012 - 06:41 PM Rate Topic: -----

#1 theprofoundgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

Creating a Dynamic HTML form using PHP

Posted 02 May 2012 - 02:40 PM

I need to create a dynamic HTML form using PHP. I am only considering the following fields : Text, Text box, Radio, Check Box, Drop down.

I am thinking of the creating the following database design.



Field ID  |  Field Type |  --- Other Attributes, such as length and others --- | Values.




In the above table, the "Values" means the values for checkbox (or any form element with multiple values) in a comma separated type.



Example :

    Table : Fields

    Field ID  | Field title |  Field Type |  --- Other Attributes, such as length and others --- | Values.

    1         | Name        | Text       |                                                       |
    2         | Color       | Checkbox   |                                                       | Blue,Red,Pink
    3         | Gender      | Radio      |                                                       | Male,Female





I could then query the above table and generate HTML accordingly, getting the values using explode function.


The other possible design that I could think off is :

    Table : Fields

    Field ID  | Field title |  Field Type |  --- Other Attributes, such as length and others --- 

    1         | Name        | Text       |                                                       
    2         | Color       | Checkbox   |                                                       
    3         | Gender      | Radio      |                                                       


    Table : Fields_options

    Field ID | Value

    2        | Blue
    2        | Red
    2        | Pink
    3        | Male
    3        | Female



I could then query the above database the generate the HTML accordingly.


In the first approach, I have the overhead of parsing the values and comma separating it using explode function. In the second approach, I have the overhead of the second query with a where clause. Which one would be better and why? Is there any better approach?

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Dynamic HTML form using PHP

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: Creating a Dynamic HTML form using PHP

Posted 02 May 2012 - 03:06 PM

The firs rule of relational database design is that no field should contain more than a single value. Doing so risks the integrity of the data, as the database will not be able to view it properly. It will consider all the values a single value, and will therefore not be able to index or filter them correctly, which will make using them more difficult than it has to be. - You should leave the control over how the data is handled with the database, and not attempt to (re)build that functionality yourself.

So, I'd suggest that you go with your second approach.

As to the overhead. Having to execute multiple queries may cost some overhead, but so will having to serialize and de-serialize the data into the fields. Which is more costly, I don't know. Would depend on the case I suppose. - It's likely a very minor concern though, overall.
Was This Post Helpful? 2
  • +
  • -

#3 theprofoundgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

Re: Creating a Dynamic HTML form using PHP

Posted 02 May 2012 - 03:21 PM

View PostAtli, on 02 May 2012 - 03:06 PM, said:

The firs rule of relational database design is that no field should contain more than a single value. Doing so risks the integrity of the data, as the database will not be able to view it properly. It will consider all the values a single value, and will therefore not be able to index or filter them correctly, which will make using them more difficult than it has to be. - You should leave the control over how the data is handled with the database, and not attempt to (re)build that functionality yourself.

So, I'd suggest that you go with your second approach.

As to the overhead. Having to execute multiple queries may cost some overhead, but so will having to serialize and de-serialize the data into the fields. Which is more costly, I don't know. Would depend on the case I suppose. - It's likely a very minor concern though, overall.


Actually... almost every CMS and Form packages go with the first approach... which is a bit confusing for me :(
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: Creating a Dynamic HTML form using PHP

Posted 02 May 2012 - 03:24 PM

Does it confuse you that many CMS packages buckle when a site takes more than a few hits?
Was This Post Helpful? 0
  • +
  • -

#5 theprofoundgeek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 09

Re: Creating a Dynamic HTML form using PHP

Posted 02 May 2012 - 03:42 PM

View PostCTphpnwb, on 02 May 2012 - 03:24 PM, said:

Does it confuse you that many CMS packages buckle when a site takes more than a few hits?


That's not my view or discussion :)

I want to know why they are doing this. Everybody is doing this! Even Community Builder (Which does not go down even after taking severe hits) does this... I want to understand the trade off there. Am I at wrong place? :)
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: Creating a Dynamic HTML form using PHP

Posted 02 May 2012 - 06:41 PM

Lot's of people are doing lots of things they shouldn't be doing, such as using outdated mysql_* functions or just trying to "get it to work" and not caring if it's efficient or safe/secure code. Look through this forum and you'll find many examples. Sometimes they get away with them, many times they don't. John Gruber has pointed out many a Word Press site that crashed when he linked to them because they often can't handle more than a user per second.

So yes, you can serialize a field and it may work, but if you need to search on that field it will be slower. Whether you can afford the speed hit will depend on your traffic and any other inefficiencies in your code and your database.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1