I’m having a problem in getting an array of a users ratings. The scenario is, I have a function that compares two arrays of numbers, and calculates how similar they are using cosine similarity.
I want to use this, to calculate the similarity of ratings from one user, to x number of other users in the database, finding the user with the most similar ratings.
The two tables relevant to this are films and ratings. Rows relevant for Film are filmID, and ratings has rows filmID, userID, and rating. for example:
filmId userID rating
1 1 3
1 3 4
2 1 2
3 1 2
2 3 3
So for user 1, I’d want an array $user1[3, 2, 2] and for user 3 $user3[4, 3, 0]
Notice that in the array for user 3, the film they HAVEN’T rated is now a zero. These arrays will then be passed to the function to calculate similarity.
I don’t know how to get the user arrays from the tables I have. I wrote some code, but am fairly stumped. So far I have:
$users = mysql_query("SELECT * FROM user");
$ratings = mysql_query("SELECT * FROM ratings");
while ($userRow = mysql_fetch_assoc($users)) {
$similarCheck = mysql_query("SELECT * FROM ratings WHERE userID =".$userRow['userID']);
$similarArray = mysql_fetch_assoc($similarCheck);
$count = 0;
while ($similarArray = mysql_fetch_assoc($similarCheck)){
$count++;
}
for ($i=0; $i < $count; $i++){
$user1 = array();
$user1[$i] = $similarArray['rating'];
}
}
Thanks for any help I do receive. If you require any further information, please let me know.
You’re not actually doing anything with your query:
This code simply loops through the query results and assigns each row to a variable, which is then overwritten over and over. When the loop exits, you’ll have a FALSE value assigned to $similarArray, since mysql_fetch calls return a boolean false when there’s no more results.
You then try to use this boolean false value in ANOTHER loop as an array which will throw all sorts of warnings.
Most likely you want something like this: