// checking whether the user has voted today:
$voted=false;
$vcheck=mysql_query(" select 1 from sort_votes
where ip='".$_server['remote_addr']."'
and date_submit=curdate()");
if(mysql_num_rows($vcheck)==1)
$voted=true;
// if we are not on the data.php?results page:
if(!array_key_exists('results',$_get))
{
echo '
- ';
// showing the tutorials by random
$res = mysql_query("select * from sort_objects order by rand()");
while($row=mysql_fetch_assoc($res))
{?>
}
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"));
if(count($data)!=$tot_objects) die("wrong data!");
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");
$maxvote=0;
$bars=array();
while($row=mysql_fetch_assoc($res))
{
$bars[]=$row;
// storing the max vote, so we can scale the bars of the chart:
if($row['votes']>$maxvote) $maxvote = $row['votes'];
}
$barstr='';
// the colors of the bars:
$colors=array('#ff9900','#66cc00','#3399cc','#dd0000','#800080');
foreach($bars as $k=>$v)
{
// buildling the bar string:
$barstr.='
}
// the total number of votes cast in the poll:
list($totvotes) = mysql_fetch_array(mysql_query("select count(*) from sort_votes"));
?>
源码下载