|
/* Database config */
$db _host = ';
$db _user = ';
$db _pass = ';
$db _database = ';
/* End Config */
$link = MySQL Tutorial _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 ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>jquery PHP Ajax Voting program source code </title>
<link rel= "stylesheet" type= "text/css Course" href= "Demo.css"/>
<script type= "text/web Effects" 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>
<body>
<div id= "Main" >
<?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" >
'/>
<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 have to style another separate page
?>
<div class= "Clear" ></div>
<!--The form below is isn't directly available to the user-->
<form action= "? Results" id= "Sform" method= "POST" >
<input name= "Sortdata" id= "Sortdata" type= "hidden" value= ""/>
</form>
</body>
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 would limit voting to once a day 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 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>
SOURCE download
|