Welcome Guest, Not a member yet? Register   Sign In
storing multiple select in database
#1

[eluser]peter222[/eluser]
I want to store my multiselect dropdown i one row as value for example type_id = '1,3,5'. Later, I need to ask database what records belongs to type = 3, how to do it? I can't do it by Mysql IN because it will select records with type_id = '4,33,1'. How to do it?
#2

[eluser]smilie[/eluser]
There are probably better ways, but only one I can think of at this moment is to use json_encode and json_decode, so that you can then search through the object...

There was another PHP function to do similar thing, but I can't remember it now Sad It also creates array...

Cheers,
Smilie
#3

[eluser]internut[/eluser]
serialize ?
#4

[eluser]gwelter[/eluser]
If you want to do this in a DB query, I think you would use

Code:
WHERE type_id LIKE '3' OR type_id LIKE '3,%' OR type_id LIKE '%,3,%' OR type_id LIKE '%,3'
#5

[eluser]peter222[/eluser]
Thanks gwelter, I will try this way. I need it to repopulate multiple select form (from one row of database). You do it this way too?
#6

[eluser]gwelter[/eluser]
I might have tried something like this in the past, but I found in the long run it's better to maintain data integrity in the database and take care of this kind of parsing in code. The more elegant way to achieve this would be to create a joiner table (since this table and the Types table are n-to-n) and create a PHP function that will return the multiselect dropdown values.
#7

[eluser]Twisted1919[/eluser]
This a a bad design decision.
Normalize your database, otherwise, in time, you will have real problems working with it .
#8

[eluser]peter222[/eluser]
OK, I decided to put it into other related database table
#9

[eluser]Unknown[/eluser]
[quote author="smilie" date="1296062363"]There are probably better ways, but only one I can think of at this moment is to use json_encode and json_decode, so that you can then search through the object...

There was another PHP function to do similar thing, but I can't remember it now Sad It also creates array...

Cheers,
Smilie[/quote]

PHP's explode() will turn a list into an array. Is this what you were referring to?




Theme © iAndrew 2016 - Forum software by © MyBB