Jump to content

If You Like This, Then You Will Like This


n8w

Recommended Posts

I am trying to figure out the logic / mysql query I need to figure out "related suggestions"

 

So for example I have these illustrators http://www.illustrationmundo.com/illustrators.php

and if you like illustrator A you will probably like illustrator B,D,Z based on a table that contains people's favorite illustrators

 

the illustrator favorite table looks like this

 

illustrator_favorites

  • user_id=the user who is favoriting the illustrator
  • illustrator_id=the illustrator that is being favorited

So I assume I need to figure out people that like illustrator 12547 usually also likes illustrators 124,125478 and 15429

 

I have no idea how to do this .. any help is greatly appreciated

Link to comment
Share on other sites

Thanks Barand .. that was a ton of help

 

Now I added a varchar field to my illustrator_table called related_illustrators which I plan to store a string of the related illustrator ids

example

123,23454,2345,1232

 

I want to run a cron job once a day to store these values

 

here is my cron job and I know it's super inefficient .. could you take a look at it to help me optimize it? Any help greatly appreciated

 



$start ="";
$querytime="";
$start = microtime(TRUE);



#############################
# BEGIN: related illustrators
#############################
$sql = 'UPDATE illustrators_table SET  related_illustrators=""'; //initialize
$retid = mysql_query($sql) or die(mysql_error());
$sql = 'SELECT illustrator_id, COUNT(*) as favorite_count FROM favorites GROUP BY illustrator_id';
$retid = mysql_query($sql) or die(mysql_error());
# check for errors
if (!$retid) {
die(mysql_error());
} else {
while ($row = mysql_fetch_array($retid)) {
 $illustrator_id = $row[illustrator_id];
 $favorite_count = $row[favorite_count];
 if($favorite_count>0){
  $sql2 = 'SELECT p.illustrator_id, count(*) as favorite_count FROM favorites p
 INNER JOIN (SELECT user_id FROM favorites WHERE illustrator_id = '.$illustrator_id.') as X using (user_id )
 WHERE illustrator_id  <> '.$illustrator_id.' GROUP BY illustrator_id
 ORDER BY favorite_count DESC LIMIT 17';

 $retid2 = mysql_query($sql2) or die(mysql_error());
 while ($row = mysql_fetch_array($retid2)) {
  $related_illustrators .=", ". $row[illustrator_id];
 }



 $sql3 = 'UPDATE illustrators_table SET'
 . ' related_illustrators = "'.$related_illustrators.'"'
 . ' WHERE'
 . ' illustrator_id = "'.$illustrator_id.' " LIMIT 1';
 //$retid3 = mysql_query($sql3) or die(mysql_error());
 }
}
}


$querytime = microtime(TRUE) - $start;
echo $querytime;


Link to comment
Share on other sites

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.