|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
string to array then to convert into a matching name
Hi all,
Please help with this problem. I want to retrieve a data from postgreSQL then convert it with a specified name. say I have a form inputting data. <form> <input type="checkbox" name="id[]" value="1">apple <input type="checkbox" name="id[]" value="2">orange <input type="checkbox" name="id[]" value="3">banana </form> table1:fruits fruits_id fruits_name 1 apple 2 orange 3 banana table2:list list_id fruits_select(*varchar) 1 1 2 2 3 1 3 4 1 2 3 then, I want to show the list like this. list_id fruits_name 1 apple 2 orange 3 apple banana 4 apple orange banana Do I use preg_split to split the string into array then count the array length??? Thanks!! |
|
#2
|
|||
|
|||
|
RE: string to array then to convert into a matching name
Hey Mic,
Looking at what you're trying to do, I am thinking the database schema could be modified to better support this. Sure it's possible to do what you want with the current setup (e.g. exploding the list records and then looking up the value, etc.), but I might suggest this instead... You have a many-to-many relationship between fruit and lists e.g. apple appears in many lists, and each list can contain many fruit. So, the "typical" way to resolve a many-to-many relationship is to use an intermediate table. Your singular tables would then only contain information about the single items: table1:fruits fruits_id fruits_name 1 apple 2 orange 3 banana table2:lists list_id list_description 1 Apple Cup 2 Orange Cup 3 Lite Cocktail 4 Full Cocktail Notice that fruits only talks about the core fruit items, and lists only talks about the list itself, not the fruit contained. Then build this intermediate table: table3: fruits_list list_id fruit_id 1 1 2 2 3 1 3 3 4 1 4 2 4 3 With a schema like this, you can join the tables in a query and iterate over the fruits_list records to get your desired result. Hope this helps. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > string to array then to convert into a matching name |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|