I have to table, contacts and groups.
Inside of contacts I have 3 columns: id first_name last_last
Inside groups I have 2 columns: id linked
I want to put contacts into groups. So I thought the best way would be that when you added a contact to a group it would add that contacts ID to a list of contact ID's separated by commas in the linked column.
So if you added contact 1, 4, 14, and 24 to a specific group the linked column would have this value 1,4,14,24
Now I want to display the contacts in that group. So I explode() the linked value and then use a foreach() to cycle through the exploded array and on each loop select through MySQL the first name and last name of that contact and display it.
All of that works fine. My problem is I realized I want to sort the contacts being displayed in that group. But they display in the order they were placed in the group.
I want to be able to sort by first name ascending or descending but I have no idea how because I'm selecting each contact in a foreach() and echoing it.
P.S. I thought maybe make a column on the contact called groups and store all the groups that contact is in the same way I stored all the contacts that were in a group. So if the contact was in groups 1, 2, 4, and 6 then that contacts groups column would have a value of 1,2,4,6. The only problem is, lets say I'm search for all contacts in group 6, how do I search though that list in a MySQL statement? I thought the best way would be SELECT first_name FROM contacts WHERE groups LIKE '%6%' but then what if a contact is in group 60? Wouldn't it select that contact too even if it isn't in the right group?