SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Wales
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to store checkbox values in a DB without ...

    ... using implode? I've a form with vehicle specification options like this - Radio CD, Tinted windows, Leather seats etc. Now the user will select, via checkboxes, each which apply to his vehicle. How best to store them in a DB without using implode (although this would be the easiest)? Anyone got any thoughts on this?

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have a few options... the two that come immediately to my mind, are a.) implode, which you don't want... but I'd recommend it. and b.) serialize the array.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have a column for each field and set the value to either Y or N

  4. #4
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a column for each field but use BIT as the data type and use 1 for when the box is checked and 0 when it isn't. For MySQL before version 5.0.3 you will need to use TINYINT(1) as BIT wasn't available.

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or enum with '0', '1' as values, much of a muchness.

    or reference another DB table and create a 1:N relationship between vehicles and features.
    For each feature, for a given vehicle a record would exist in the `feature` DB referencing the main vehicle DB table with a foreign key.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,608
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ^^^ What he said ^^^

    In fact, you could have a Cars table, a Features table (wherein each feature has an ID number - allows you to easily expand the feature set), and a FeaturesToCars linking table which has CarID and FeatureID fields - e.g. if CarID 23 has 5 of the features then there will be 5 rows in this table with CarID = 23, and the value of the FeatureID in those rows will relate to the feature from the Features table.

    This is good normalisation and lets you use JOINs so that the database can do all the work when you want to retrieve info.
    Ian Anderson
    www.siteguru.co.uk


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •