CodeIgniter Forums
storing multiple select in database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: storing multiple select in database (/showthread.php?tid=37960)



storing multiple select in database - El Forum - 01-26-2011

[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?


storing multiple select in database - El Forum - 01-26-2011

[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


storing multiple select in database - El Forum - 01-27-2011

[eluser]internut[/eluser]
serialize ?


storing multiple select in database - El Forum - 01-28-2011

[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'



storing multiple select in database - El Forum - 01-29-2011

[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?


storing multiple select in database - El Forum - 01-29-2011

[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.


storing multiple select in database - El Forum - 01-30-2011

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


storing multiple select in database - El Forum - 02-01-2011

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


storing multiple select in database - El Forum - 02-01-2011

[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?