GAURAV KUMAR

Monday, November 22, 2010

facemash experience "the social network"

hello people,
I have been constantly watching and listening "The Social Network" since the time I first watched it.Awesome movie.very inspirational.Well,I too thought about writing facemash after watching this.But,I did not want to end up facing punishment like mark.So, I didn't try to insult anyone.Just experiment and wanted to refresh my php and mysql concepts.


What we need is collection of some images.put it in a directory and provide its path in the code.for this example I will use "pics" as directory which has 7 images.Now write one php script to display 2 images randomly and let user click on any of this.Now,one more script to check which image was clicked and calculate the new rank of both image.I am sharing a piece of code here.

require_once("sql.php"); //sql.php connects to the database.
srand(time());
$r1 = rand()%7;
srand(time());
$r2 = rand()%7 ;
while($r1 == $r2)
{
srand(time()) ;
$r2 = rand()%7 ;
}
$query1 = "SELECT * FROM pic WHERE picid = '$r1'";
$result1 = mysql_query($query1);
$row1 = mysql_fetch_array($result1);
$query2 = "SELECT * FROM pic WHERE picid = '$r2'";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_array($result2);
$pic1 = $row1['pic'] ;
$pic2 = $row2['pic'] ;

give image with its link in the html part of the code href='mash.php?id1=$r1&id2=$r2' img src=pics/".$pic1


href='mash.php?id1=$r2&id2=$r1'img src=pics/".$pic2


now when this page redirects to mash.php,new rank of both images is calculated based on the algorithm you wanna use.here, we have defined 3 variables.'h' is no. of hit on the image,'d' no. of times the image has been displayed.'rank' shows point given to image.use get method to catch variables to update database containing no. of hits of an image.
$id1 = $_GET['id1'];
$id2 = $_GET['id2'];

$query1 = "SELECT * FROM pic WHERE picid = '$id1'";
$result1 = mysql_query($query1);
$row1 = mysql_fetch_array($result1);
$query2 = "SELECT * FROM pic WHERE picid = '$id2'";
$result2 = mysql_query($query2);
$row2 = mysql_fetch_array($result2);

$h1 = $row1['h']+1;
$d1 = $row1['d']+1;
$rank1 = [algo to calculate new rank];

$h2 = $row2['h']; // no. of hit remains same for the image not clicked.
$d2 = $row2['d']+1;
$rank2 = [algo to calculate new rank] ;

$res1 = mysql_query("UPDATE pic SET h='$h1',d='$d1',rank='$rank1' WHERE picid='$id1' ") ;
$res2 = mysql_query("UPDATE pic SET h='$h2',d='$d2',rank='$rank2' WHERE picid='$id2' ") ;


So,this was my experience.Worked without any problem.See ya.