I am working on a PHP application that finds all possible ways to swap cards. each user has at least one card. When a user request a card, the application shows him all possible ways to swap his card in excange for the card that he requested.
Lets say that:
user 1 has card type A
user 2 has card type B
user 3 has card type C
And lets assume that:
user 1 wants card type C
user 2 wants card type A
user 3 wants card type B
If user 1 requests card C, the only solution is that:
user 1 gives user 2 his card
user 2 gives user 3 his card
user 3 gives user 1 his card
But what if there were two other users:
user 4 has card type B & wants card type A
user 5 has card type C & wants card type B
Now, If user 1 requests card C, there is one other solution besides the one above which is that :
user 1 gives user 4 his card
user 4 gives user 5 his card
user 5 gives user 1 his card
There will be NO limit in terms of how many cards a user may have or request. So far, I created two SQL tables. The table “cards” keeps track of each userID and cardID. The Table “requests” keeps track of each userID and the desired cardID.
I wouldn’t use pure PHP for this. Rather I would create a MySQL table which stores a record for each user, recording which card that user has as well as which card he wants. Then, I would run something like this:
If I were to do this using plain PHP, I would do something like this: