/* database config */
$db_host = '';
$db_user = '';
$db_pass = '';
$db_database = '';
/* end config */
$link = mysql教程_connect($db_host,$db_user,$db_pass) or die('unable to establish a db connection');
mysql_select_db($db_database,$link);
mysql_query("set names utf8");
?>
<!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=gb2312" />
<title>jquery php ajax 投票程序源码</title>
<link rel="stylesheet" type="text/css教程" href="demo.css" />
<script type="text/网页特效" src="">http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">>
<script type="text/javascript" src="">http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js">>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="main">
<hr />
<?php
// 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 '<ul class="sort">';
// showing the tutorials by random
$res = mysql_query("select * from sort_objects order by rand()");
while($row=mysql_fetch_assoc($res))
{?>
<li id="li<?php echo $row['id']?>">
<div class="tut">
<div class="tut-img">
<img src="<?php echo $row['img']?>" width="100" height="100" alt="<?php echo $row['title']?>" />
<div class="drag-label"></div>
</div>
<div class="tut-title">
<a href="<?php echo $row['url']?>" target="_blank" title="open it in a new window!"><?php echo $row['title']?></a>
</div>
<div class="tut-description"><?php echo $row['description']?></div>
<div class="clear"></div>
</div>
</li>
<?php } ?>
</ul>
<div class="button-holder">
<?php if(!$voted):?><a href="" id="submitpoll" class="button">submit poll<span></span></a><?php endif;?>
<a href="?results" class="button">view the results<span></span></a>
</div>
<?php
}
else require "results.php";
// the above require saves us from having to style another separate page
?>
<div class="clear"></div>
<!-- the form below is not directly available to the user -->
<form action="?results" id="sform" method="post">
<input name="sortdata" id="sortdata" type="hidden" value="" />
</form>
</body>
</html>
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.='
<div class="bar" style="width:'.max((int)(($v['votes']/$maxvote)*450),100).'px;background:'.$colors[$k].'">
<a href="'.$v['url'].'" title="'.$v['title'].'">'.$v['short'].'</a>
</div>';
}
// the total number of votes cast in the poll:
list($totvotes) = mysql_fetch_array(mysql_query("select count(*) from sort_votes"));
?>
<div class="chart">
<?php echo $barstr?>
</div>
<a href="demo.php" class="button">go back<span></span></a>
<div class="tot-votes"><?php echo $totvotes?> votes</div>
源码下载
|