Recursively linking records
I have a table with timeslots that users can sign up for. On the administrative end, the admin-user can choose to 'link' two timeslots together, which basically says to the end-user, 'If you sign up for one of these timeslots, you must sign up for all other timeslots that are linked to it.' These links are stored in another table which has two columns, one being the ID of the timeslot requiring a link, and the other being开发者_运维问答 the ID of the timeslot that is being linked to.
How can I make it such that when linking one timeslot with another, BOTH depend on the other being selected. This needs to be recursive for other linked timeslots as well, like the following example:
Admin says:
#1 is linked to #2
#3 is linked to #2
Therefore:
#1 is linked to #2
#2 is linked to #1
#3 is linked to #2
#2 is linked to #3
#3 is linked to #1
#1 is linked to #3
What is the best way to accomplish this? Right now I'm trying to put that information into a link table, but if there's another way that I can do it with more PHP and less SQL that would work also. I would provide a code sample, but I don't think that it would be helpful.
Edit: Conceptual answers are fine. I don't need code written for me unless for demonstrative purposes.
One approach may be a table of two columns in the form of: Node_Id, and Link_Id.
In this case, Node_Id is the Timeslot ID. For the example above, the rows would be:
1 1
2 1
3 1
Where 1, 2, 3 in the first column are timeslots, and the 1 in the second is the link.
Hypothetically let's add timeslots 4, 5, 6, 7, and 8 with links 2, 3, and 4.
4 2
5 3
6 2
7 4
8 2
This translates to:
Pick 4, must also pick 6 and 8 (all are link 2).
Pick 5, you're done (only member of link 3).
Pick 6, must also pick 4 and 8 (all are link 2).
Pick 7, you're done (only member of link 4).
Pick 8, must also pick 4 and 6 (all are link 2).
Easiest way might be to make a timeslot group table. You would then have something like:
TIMESLOT_GROUPS
group_no group_label
-------- ----------
1 whatever
TIME_GROUPS
timeslot_no group_no
----------- --------
1 1
2 1
3 1
It's then very easy to see which other timeslots are related to the current one. You don't actually need the timeslot_groups table if you have no information (label, etc.) to store about the grouping. You just need to make sure to keep your group numbers unique. (My guess is that a "timeslot group" is a "course" or a "class", so you probably already have this table).
精彩评论