Forum Moderators: coopster

Message Too Old, No Replies

Storing multiple numbers

         

smatts9

8:07 pm on Jul 15, 2006 (gmt 0)

10+ Year Member



I have a mysql table full of items, and I have a field called "related". Each item has a unique id called id. I'm wondering what's the best way to store the id's of related items?

id ¦ name ¦ related
1 ¦ name1 ¦ 2,4,9
2 ¦ name2 ¦ 12,1,4
3 ¦ name3 ¦ 9,29,7
4 ¦ name4 ¦ 34,2,2

etc, etc, etc. There are more fields but unimportant for what I am trying to accomplish.

Is this a good way of storing them? If not what is? And how would I be able to use that info usefully? Can you write a PHP script to separate them? I'm thinking of using a script with while() to go through and list all the items that are related with their name and description. Thanks for your help.

coopster

10:13 pm on Jul 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You could certainly do it that way, but I'm not sure that is the way I would go about it. I would separate the relateds to their own table, a cross-reference of sorts.
ids
id ¦ name 
1 ¦ name1
2 ¦ name2
3 ¦ name3
4 ¦ name4

relateds
id ¦ related 
1 ¦ 2
1 ¦ 4
1 ¦ 9
2 ¦ 12
2 ¦ 1
2 ¦ 4
3 ¦ 9
3 ¦ 29
3 ¦ 7
4 ¦ 34
4 ¦ 2

smatts9

11:04 pm on Jul 15, 2006 (gmt 0)

10+ Year Member



ok sounds good, thanks.