Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3441292
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:32:07+00:00 2026-05-18T08:32:07+00:00

I am working on a PHP application that finds all possible ways to swap

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:32:08+00:00Added an answer on May 18, 2026 at 8:32 am

    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:

    $sql = "SELECT * FROM users";
    $result = $this->db->query();
    $users = $result->getAll(); //A list of all the users.
    
    foreach ($users as $user)
    {
       $sql = "SELECT * FROM users WHERE cardId = '$user->wantedCardId'";
       $result = $this->db->query($sql);
    
       if (! $result->has_rows())
       {
          echo "No other users with $user->wantedCardId were found.";
          continue;
       }
    
       $cardHolder = $result->row();
       echo: "User $cardHolder->id gives $user->id his card";
    }
    

    If I were to do this using plain PHP, I would do something like this:

    //Populate an array containing a list of all the users and their cards.
    
    $users = array();
    
    $user[1] = new StdClass();;
    $user[1]->cardId = 2;
    $user[1]->wantedId = 3;
    
    $user[2] = new StdClass();
    $user[2]->cardId = 3;
    $user[2]->wantedId = 2;
    // .....
    
    foreach ($users as $userId=>$user)
    {
       //Run a secondary loop through the users to find those that have the card that
       //this user wants.
       foreach ($users as $holderId=>$cardHolder)
       {
          if ($cardHolder->cardId != $user->wantedId)
             continue;
          echo "User $holderId gives $userId his card";
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a PHP application that links into the Protx VSP Direct payment
I am working on a PHP application that intends to ease company workflow and
Hey everyone, I'm working on a PHP application that needs to parse a .tpl
I am currently working on a web application that uses PHP and MySQL, but
I am working on a huge php application with Zend framework, in that project
I'm currently working on an application that uses php's simplexml to interpret rss feeds
I'm working profesionally on a php web application which contains contacts, among other data.
I'm working on developing a native android application to retrieve data for a user
I am working to an application that uses jqGrid. The problem is that the
I'm building my first application that requires a calendar. I have it working great

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.