Jump to content

Recommended Posts

Trying to print out the user with the most similarity with another user.

 

Based on User A's preferences find the user with the most similar taste and print them in order, but do not print user with nothing in common.

 

User A likes blue, green, red, orange, purple

 

--

 

User B likes blue, green

User C likes orange, purple

User D likes green, yellow, gold

User E likes orange, blue, green, silver

User F likes yellow, cyan, gold

 

Based on those, I would like it to print as follows (in order).

 

User E (3 similar)

User B (2 similar)

User C (2 similar)

User D (1 similar)

 

DO NOT PRINT User F since that user has no similar taste.

 

* Profiles list all users' profiles with the primary key being profile_id

* Traits are a list of traits (think tagging) input by users with trait_id as the primary key

* Matches contain 2 fields (profile_id and trait_id).

 

So, essentially if bettysue (profile_id = 200) tags herself with "nerdy" (trait_id = 10) in the matches table there would be a row with profile_id = 200 and trait_id = 10.

 

A trait can have more than one profile just as a profile can have more than 1 trait assign to them. Makes sense?

 

 

 

Any tips? I think a for loop is needed along with an array but getting it started is difficult for me.

 

 

Link to comment
https://forums.phpfreaks.com/topic/46485-select-record-with-most-similar-fields/
Share on other sites

Use array_intersect() to get common values and count the resulting array

 

<?php
$A = array ('blue', 'green', 'red', 'orange', 'purple');

$others = array (
    'B' => array ('blue', 'green'),
    'C' => array ('orange', 'purple'),
    'D' => array ('green', 'yellow', 'gold'),
    'E' => array ('orange', 'blue', 'green', 'silver'),
    'F' => array ('yellow', 'cyan', 'gold' )
);
$results = array();

foreach ($others as $k => $prefs) {
    $similar = array_intersect($A, $prefs);
    $results[$k] = count($similar);
}
arsort ($results);

foreach ($results as $k => $n) {
    if ($n > 0)
        echo "User $k ($n similar)<br>";
}
?>
[[code]

[/code]

You just need a single, somewhat complex query - you never want to have looping with queries if you can help it. Since I don't know the actual names of your tables and columns I just guessed. You should be able to figure it out:

 

<?php

$userID; //the id of the usre to find matches against

$sql = "SELECT p1.profile_name, COUNT(p1.profile_id) as trait_count
       FROM profiles p1, traits t1
       WHERE p1.profile_id = t1.profile_id

         AND p1.profile_id <> '$userID'

         AND t1.trait_id IN (
           SELECT t2.trait_id
           FROM traits t2
           WHERE t2.profile_id = '$userID'
         )

       GROUP BY p1.profile_name
       ORDER BY trait_count DESC";

?>

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.