Kamis, 21 April 2011

Simple PHP dan MSQL poll

Making the Database

This tutorial will demonstrate how to make a basic poll using PHP, and store the results in MySQL. We will then display the results by making a pie chart with the GD Library.

The first thing we must do is create a database. Our example our poll will have three options, however you can modify this to fit your needs.


CREATE TABLE votes (first INTEGER, sec INTEGER, third INTEGER);
INSERT INTO votes (first, second, third) VALUES (0,0,0)


Voting Script - Part 1


// Connects to your Database
mysql_connect("your_server", "your_login", "your_pass") or die(mysql_error());
mysql_select_db("your_database") or die(mysql_error());

//Name of our cookie
$cookie = "Voted";

//A function to display our results - this refrences vote_pie.php which we will also make
function pie ()
{
$data = mysql_query("SELECT * FROM votes")
or die(mysql_error());
$result = mysql_fetch_array( $data );
$total = $result[first] + $result[sec] + $result[third];
$one = round (360 * $result[first] / $total);
$two = round (360 * $result[sec] / $total);
$per1 = round ($result[first] / $total * 100);
$per2 = round ($result[sec] / $total * 100);
$per3 = round ($result[third] / $total * 100);
echo "
";
Echo "FIRST = $result[first] votes, $per1 %

SECOND = $result[sec] votes, $per2 %

THIRD = $result[third] votes, $per3 %
";
}


We start out or script with the information we need to connect to our database.

We then name our cookie, and define a function called pie. In our pie function we retrieve the data from our database. We also preform a few calculations that help us display the results in a user friendly way, such as the percentage each vote has, and how many degrees out of 360 that percentage makes up. We reference vote_pie.php which we will create later in the tutorial.

Voting Script - Part 2


//This runs if it is in voted mode
if ( $mode=="voted")
{

//makes sure they haven't already voted
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already voted this month
";
}

//sets a cookie
else
{
$month = 2592000 + time();
setcookie(Voted, Voted, $month);

// adds their vote to the database
switch ($vote)
{
case 1:
mysql_query ("UPDATE votes SET first = first+1");
break;
case 2:
mysql_query ("UPDATE votes SET sec = sec+1");
break;
case 3:
mysql_query ("UPDATE votes SET third = third+1");
}

//displays the poll results
pie ();
}
}


The next section of code runs if our voting form has been submitted.

It first checks the user to see if they already have a voted cookie. If they do, it does not let them vote again and gives them an error message. However, if they do not, it sets the cookie in their browser and then adds their vote to our database. Finally, it displays the results of the poll by running our pie function.

Voting Script - Part 3


//if they are not voting, this displays the results if they have already voted
if(isset($_COOKIE[$cookie]))
{
pie ();
}

// or if they have not voted yet, they get the voting box
else
{
if(!$mode=='voted')
{
?>






}
}
?>


The final part of the script runs if they are not in voting mode.

It checks to see if they have a cookie in their browser. If they do, then it knows they have already voted and displays the poll results for them. If there is no cookie, it then checks to make sure they aren't in voted mode. If they are, then nothing happen, but if they are not it displays the form that lets them vote.
It is a good idea to include this poll on your page using the include function. Then you can place the poll anywhere you want within the page, simply using one line.


INCLUDE 'http://www.yoursite.com/path/to/poll.php' ;


Using GD Library


$slide = $one + $two;
header('Content-type: image/png');
$handle = imagecreate(100, 100);
$background = imagecolorallocate($handle, 255, 255, 255);
$red = imagecolorallocate($handle, 255, 0, 0);
$green = imagecolorallocate($handle, 0, 255, 0);
$blue = imagecolorallocate($handle, 0, 0, 255);
$darkred = imagecolorallocate($handle, 150, 0, 0);
$darkblue = imagecolorallocate($handle, 0, 0, 150);
$darkgreen = imagecolorallocate($handle, 0, 150, 0);

// 3D look
for ($i = 60; $i > 50; $i--)
{
imagefilledarc($handle, 50, $i, 100, 50, 0, $one, $darkred, IMG_ARC_PIE);
imagefilledarc($handle, 50, $i, 100, 50, $one, $slide , $darkblue, IMG_ARC_PIE);
imagefilledarc($handle, 50, $i, 100, 50, $slide, 360 , $darkgreen, IMG_ARC_PIE);
}
imagefilledarc($handle, 50, 50, 100, 50, 0, $one , $red, IMG_ARC_PIE);
imagefilledarc($handle, 50, 50, 100, 50, $one, $slide , $blue, IMG_ARC_PIE);
imagefilledarc($handle, 50, 50, 100, 50, $slide, 360 , $green, IMG_ARC_PIE);
imagepng($handle);
?>


In our script we called vote_pie.php to display the pie chart of our results.

The above code should be placed in the vote_pie.php file. Basically what this does is draw arcs to create a pie. We passed the variables it needed in the link from our main script. To better understand this code, you should read our GD tutorial that covers arcs and pies.

0 komentar:

Posting Komentar

Diberdayakan oleh Blogger.
 
 
Copyright © Bengkel Web
2011 All Reserved
Devilzc0de Simple Template by Awang Shellovers