A Simple Slope One User Item Recommender In PHP/MySQL

Big fan of user/item recommenders, which is also known as collaborative filtering. Slope one is a very basic but reasonably accurate recommender algorithm which makes it fairly simple to implement in PHP/MySQL. I attempted to make a reusable algo you could use in your application. This implementation of slope one is basically a simplistic netflix algorithm that takes users, items, and ratings and predicts what the user might rate on known items they have not yet rated. Slope one was first proposed by Daniel Lemire and Anna Maclachlan.

Something interesting to note about recommenders though, I recall from a netflix paper I read that implicit user ratings produce better recommendations than explicit. Meaning the movies people watch, their actual behavior, is more useful in prediction than what they self report as a rating.

Create a pdo connection to pass in

Create instance of SlopeOne

Create the 2 tables we need to store data, caution, only run once because it will drop and recreate the tables.

Add some users, items, and their ratings. Note that I purposely left out Sara’s rating for Salads, the item we’re trying to predict for her.


Clear out advice table (all predictions)


Process all ratings into advice (predictions)


Show all predictions for user Sara


Sara’s salad prediction


SlopeOne class