}
else require "results.php";
// the above require saves us from having to style another separate page
?>
results.php
if($_post['sortdata'])
{
// the data arrives as a comma-separated string,
// so we extract each post ids:
$data=explode(',',str_replace('li','',$_post['sortdata']));
// getting the number of objects
list($tot_objects) = mysql_fetch_array(mysql_query("select count(*) from sort_objects"));
foreach($data as $k=>$v)
{
// building the sql query:
$str[]='('.(int)$v.','.($tot_objects-$k).')';
}
$str = 'values'.join(',',$str);
// this will limit voting to once a day per ip:
mysql_query(" insert into `sort_votes` (ip,date_submit,dt_submit)
values ('".$_server['remote_addr']."',now(),now())");
// if the user has not voted before today:
if(mysql_affected_rows($link)==1)
{
mysql_query(' insert into `sort_objects` (id,votes) '.$str.'
on duplicate key update votes = votes+values(votes)');
}
}
// selecting the sample tutorials and ordering
// them by the votes each of them received:
$res = mysql_query("select * from sort_objects order by votes desc");