I downloaded the script just for fun and for learning some new things.
For the one who are curious what this is: http://facemash.moefelt.dk/

I wanted to install this on my localhost, but i got this: http://imageshack.us/f/41/facemashalikenotgood.png/

This is my index.php code:

<?php

/*
 * Title: Facemash-Alike Script
 * Author: Anders Moefelt / http://moefelt.dk
 * Version: 1.0
 * 
 * Great resources:
 *
 * 1) http://www.jasonhuber.net/the-social-network-rating-formula-elo/
 * 2) http://www.imdb.com/title/tt1285016/
 * 3) http://en.wikipedia.org/wiki/Mark_Zuckerberg
 * 4) http://www.facebook.com/markzuckerberg
 *
 * Performance rating = [(Total of opponents' ratings + 400 * (Wins - Losses)) / score].
 */

include('mysql.php');
include('functions.php');


// Get random 2
$query="SELECT * FROM images ORDER BY RAND() LIMIT 0,30";
$result = @mysql_query($query);

while($row = mysql_fetch_object($result)) {
	$images[] = (object) $row;
}


// Get the top10
$result = mysql_query("SELECT *, ROUND(score/(1+(losses/wins))) AS performance FROM images ORDER BY ROUND(score/(1+(losses/wins))) DESC LIMIT 0,10");
while($row = mysql_fetch_object($result)) $top_ratings[] = (object) $row;


// Close the connection
mysql_close();


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Facemash-Alike Script</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
</head>

<body>


<h1>FACEMASH-ALIKE</h1>
<h3>Were we let in for our looks? No. Will we be judged on them? Yes.</h3>
<h2>Who's hotter? Click to choose.</h2>

<center>
<table>
	<tr>
		<td valign="top" class="image"><a href="rate.php?winner=<?=$images[0]->image_id?>&loser=<?=$images[1]->image_id?>"><img src="images/<?=$images[0]->filename?>" /></a></td>
		<td valign="top" class="image"><a href="rate.php?winner=<?=$images[1]->image_id?>&loser=<?=$images[0]->image_id?>"><img src="images/<?=$images[1]->filename?>" /></a></td>
	</tr>
	<tr>
		<td>Won: <?=$images[0]->wins?>, Lost: <?=$images[0]->losses?></td>
		<td>Won: <?=$images[1]->wins?>, Lost: <?=$images[1]->losses?></td>
	</tr>
	<tr>
		<td>Score: <?=$images[0]->score?></td>
		<td>Score: <?=$images[1]->score?></td>
	</tr>
	<tr>
		<td>Expected: <?=round(expected($images[1]->score, $images[0]->score), 4)?></td>
		<td>Expected: <?=round(expected($images[0]->score, $images[1]->score), 4)?></td>
	</tr>
</table>
</center>

<h2>Top Rated</h2>
<center>
<table>
	<tr>
		<? foreach($top_ratings as $key => $image) : ?>
		<td valign="top"><img src="images/<?=$image->filename?>" width="70" /></td>
		<? endforeach ?>
	</tr>
	<? 
	<tr>
		<? foreach($top_ratings as $key => $image) : ?>
		<td valign="top">Score: <?=$image->score?></td>
		<? endforeach ?>
	</tr>
	<tr>
		<? foreach($top_ratings as $key => $image) : ?>
		<td valign="top">Performance: <?=$image->performance?></td>
		<? endforeach ?>
	</tr>
	<tr>
		<? foreach($top_ratings as $key => $image) : ?>
		<td valign="top">Won: <?=$image->wins?></td>
		<? endforeach ?>
	</tr>
	<tr>
		<? foreach($top_ratings as $key => $image) : ?>
		<td valign="top">Lost: <?=$image->losses?></td>
		<? endforeach ?>
	</tr>
	 ?>
</table>
</center>

</body>
</html>

The css is something i made myself because the style was in the index.php i thought it was better to make a external css. So this is the main.css (got the style+code from the script)

body, html {
font-family:Arial, Helvetica, sans-serif;width:100%;margin:0;padding:0;text-align:center;
}

h1 {
background-color:#600;color:#fff;padding:20px 0;margin:0;
}

a img {
border:0;
}

td {
font-size:11px;
}

.image {
background-color:#eee;border:1px solid #ddd;border-bottom:1px solid #bbb;padding:5px;
}

Functions.php

<?php

// Calculate the expected % outcome
function expected($Rb, $Ra) {
	return 1/(1 + pow(10, ($Rb-$Ra)/400));
}

// Calculate the new winnner score
function win($score, $expected, $k = 24) {
	return $score + $k * (1-$expected);
}

// Calculate the new loser score
function loss($score, $expected, $k = 24) {
	return $score + $k * (0-$expected);
}

?>

Installimages.php (I already runned this script, this is working, there are images in my database)

<?php

include('mysql.php');

if ($handle = opendir('images')) {

	/* This is the correct way to loop over the directory. */
	while (false !== ($file = readdir($handle))) {
		if($file!='.' && $file!='..') {
			$images[] = "('".$file."')";
		}
	}

	closedir($handle);
}

$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
	print mysql_error();
}
else {
	print "finished installing your images!";
}


?>

Rate.php

<?php


include('mysql.php');
include('functions.php');


// If rating - update the database
if ($_GET['winner'] && $_GET['loser']) {


	// Get the winner
	$result = mysql_query("SELECT * FROM images WHERE image_id = ".$_GET['winner']." ");
	$winner = mysql_fetch_object($result);


	// Get the loser
	$result = mysql_query("SELECT * FROM images WHERE image_id = ".$_GET['loser']." ");
	$loser = mysql_fetch_object($result);


	// Update the winner score
	$winner_expected = expected($loser->score, $winner->score);
	$winner_new_score = win($winner->score, $winner_expected);
		//test print "Winner: ".$winner->score." - ".$winner_new_score." - ".$winner_expected."<br>";
	mysql_query("UPDATE images SET score = ".$winner_new_score.", wins = wins+1 WHERE image_id = ".$_GET['winner']);


	// Update the loser score
	$loser_expected = expected($winner->score, $loser->score);
	$loser_new_score = loss($loser->score, $loser_expected);
		//test print "Loser: ".$loser->score." - ".$loser_new_score." - ".$loser_expected."<br>";
	mysql_query("UPDATE images SET score = ".$loser_new_score.", losses = losses+1  WHERE image_id = ".$_GET['loser']);


	// Insert battle
	mysql_query("INSERT INTO battles SET winner = ".$_GET['winner'].", loser = ".$_GET['loser']." ");


	// Back to the frontpage
	header('location: /');
	
}


?>

Mysql.php

<?php

// Mysql settings

$user			= "root";
$password	= "MYPASSWORD";
$database	= "facemashalike";
$host			= "localhost";

mysql_connect($host,$user,$password);
mysql_select_db($database) or die( "Unable to select database");

?>

The images are installed in my datbase(localhost) in the database facemashalike in the tab images.. So i do not know what I am doing wrong.

This is the readme file:

Hi,

Thanks for downloading this script.

It's sole purpose is having fun - and learning by doing.

I would probably never had come across the ELO rating system if it hadn't been for Marc Zuckerberg
and the movie: The social network. Great movie and a lot of fun and inspiration!

The script is not really in a production state and there is plenty room for improvement (if you make any
just mail me so I can update the source with your addons).

To get you going:

1) upload all files to your website/testspace or whereever you wanna play with it.
2) setup your database - look in the file: mysql.php
3) execute this SQL to setup your database tables:

		CREATE TABLE IF NOT EXISTS `battles` (
			`battle_id` bigint(20) unsigned NOT NULL auto_increment,
			`winner` bigint(20) unsigned NOT NULL,
			`loser` bigint(20) unsigned NOT NULL,
			PRIMARY KEY  (`battle_id`),
			KEY `winner` (`winner`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
		
		
		CREATE TABLE IF NOT EXISTS `images` (
			`image_id` bigint(20) unsigned NOT NULL auto_increment,
			`filename` varchar(255) NOT NULL,
			`score` int(10) unsigned NOT NULL default '1500',
			`wins` int(10) unsigned NOT NULL default '0',
			`losses` int(10) unsigned NOT NULL default '0',
			PRIMARY KEY  (`image_id`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

4) place all your images that's up for battle in the /imagees/ folder.
5) run the /install_images.php from your site to install all images from your folder into your database.
6) have fun!


Best regards from Denmark,

/Anders

Sorry for the long post, I wanted to sort this out :) Thank you

Recommended Answers

All 7 Replies

It did work when I put it on my website, but on my localhost it's still down?
Anybody knows why?

Member Avatar for iamthwee

The one thing I can think of is:

"Are your tables and usernames and passwords exactly the same on localhost as on your server?"

Member Avatar for diafol

Also this script uses short tags, e.g.

<? foreach($top_ratings as $key => $image) : ?>

Should use full php tags. SOme servers won't like it.

However, problems with a ready made script should be presented to the author in the first instance. Presenting use with almost 300 lines of which is mostly somebody else's code is a bit much.

hi guys. i also got the same error as mentioned in the image.please help.tell me what is wrong??help me..

Hello friends, i have one big problem (bug) with this code. Because if you copy the generate link for the voting, you may voting 3 times , 4 times .... etc. for one image. Tell me please how to fix this bug. Thankx Paolo.

Member Avatar for diafol

Unless you log the logged-in user with a vote, you can't really avoid it. You can set cookies on the user's machine to show that they've voted but cookies can be eaten. IP addresses aren't that cool either as different users may use the same machine.

votes

user_id | vote_id

should cover it.

I am agree with @diafol. You should use full tags like <?php .... ?>

or you can check this in configuration
<? ?> // short tags, need short_open_tag enabled in php.ini

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.