#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Multiple list menus into one database column


    I have 10 list menus on a page. I want to take the values selected in each menu and insert them, one row at a time, into one mysql column.

    I think an array is required, but I don't know how to get values from multiple list menus into and array and then insert these into a database.

    Any help much appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    57
    Rep Power
    12
    Hi,

    Storing an array in a table is a bad idea. It violates the principle of normalization and makes data processing difficult and slow. In MySQL, it isn't even possible without some kind of hack. You'd have to store the values in a string either using a certain data format (like JSON) or comma-separating them.

    How did you even get to this requirement? Wouldn't it make more sense to ask about a sensible way of storing the values?

    There are basically two possibilities that make sense: If the set of options is fixed and unlikely to change, you could make 10 columns (that's especially useful when you have different data types). Otherwise, create a "key" and a "value" column and store each selection in a separate row.

    Apart from those general suggestions, your question is much too vague. We need some specific code/HTML. What did you try already?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0
    I do currently have 10 columns, the problem is when I come to do the analysis, the data in those 10 columns need to be put into 1 column grouped by an ID. I can't for the life of me work out how to do this in SQL so thought the simplest way would be to take the values from the 10 list menus and insert them into 1 column.

    Maybe this is a query issue rather than PHP.

    It's actually hard to explain what it is I'm doing without showing you the forms and expected outputs, examples of which I only have in Excel spreadsheets.
  6. #4
  7. Confused badger
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2009
    Location
    West Yorkshire
    Posts
    1,046
    Rep Power
    423
    Originally Posted by stoyleg
    I have 10 list menus on a page. I want to take the values selected in each menu and insert them, one row at a time, into one mysql column.

    I think an array is required, but I don't know how to get values from multiple list menus into and array and then insert these into a database.

    Any help much appreciated.
    Name the menus the same name with [] at the end; for example:-

    <select name='mymenu[]'>
    <option value=1>1</option>
    <option value=2>2</option>
    ...
    <option value=5>5</option>
    </select>

    <select name='mymenu[]'>
    <option value=A>B</option>
    <option value=B>B</option>
    ...
    <option value=E>E</option>
    </select>


    Then, when submitted, the data is already in an array called, in this case 'mymenu', you can either write that as a lump into MySQL or do a foreach / for loop, inserting one selection at a time.
    "For if leisure and security were enjoyed by all alike, the great mass of human beings who are normally stupefied by poverty would become literate and would learn to think for themselves; and when once they had done this, they would sooner or later realise that the privileged minority had no function and they would sweep it away"
    - George Orwell, 1984

IMN logo majestic logo threadwatch logo seochat tools logo