php+mysql投票_简单PHP和MySQL投票

php+mysql投票

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

本教程将演示如何使用做一个基本的民意调查PHP和结果存储在MySQL的 。 然后,通过使用GD Library制作饼图来显示结果。

建立数据库 ( Making the Database )

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

您必须做的第一件事是创建一个数据库。 我们的示例民意测验将有三个选择。 但是,您可以对其进行修改以适合您的需求。

制作投票脚本的第一部分 ( Part One of Making a Voting Script )

You start out or script with the information you need to connect to your database. You then name your cookie and define a function called pie. In your pie function, you retrieve the data from your database. You also perform a few calculations that will help you 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. You reference vote_pie.php, which you will create later in the tutorial.

您可以使用连接到数据库所需的信息开始或编写脚本。 然后,您命名cookie并定义一个名为pie的函数。 在Pie函数中,您从数据库中检索数据。 您还可以执行一些计算,以帮助您以用户友好的方式显示结果,例如每次投票所占的百分比以及该百分比构成的360度度。 您引用的vote_pie.php,将在本教程的后面部分创建。

制作投票脚本的第二部分 ( Part Two of Making a Voting Script )

The next section of code runs if your 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 your pie function.

如果您的投票表已提交,则下一部分代码将运行。 它首先检查用户,看他们是否已经有一个投票过的cookie。 如果他们这样做,则不会让他们再次投票并给他们一条错误消息。 但是,如果没有,它将在其浏览器中设置cookie,然后将其投票添加到我们的数据库中。 最后,它通过运行Pie函数显示轮询的结果。

制作投票脚本的第三部分 ( Part Three of Making a Voting Script )

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 happens. But if they are not, it displays the form that lets them vote.

如果脚本未处于投票模式,则脚本的最后一部分将运行。 它检查他们的浏览器中是否有cookie。 如果投票通过,则表明他们已经投票,并显示投票结果。 如果没有cookie,它将检查以确保它们未处于投票模式。 如果是这样,则什么也不会发生。 但是,如果不是,它将显示让他们投票的表格。

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函数在页面上包含此民意调查。 然后,您只需使用一行即可将民意测验放置在页面中所需的任何位置。

制作投票脚本的第四部分 ( Part Four of Making a Voting Script )


header('Content-type: image/png');
header('Content-type:image / png');
$one = $_GET['one'];
$ one = $ _GET ['one'];
$two = $_GET['two'];
$ two = $ _GET ['two'];
$slide = $one + $two;
$ slide = $ one + $ two;
$handle = imagecreate(100, 100);
$ handle = imagecreate(100,100);
$background = imagecolorallocate($handle, 255, 255, 255);
$ background = imagecolorallocate($ handle,255,255,255);
$red = imagecolorallocate($handle, 255, 0, 0);
$ red = imagecolorallocate($ handle,255,0,0);
$green = imagecolorallocate($handle, 0, 255, 0);
$ green = imagecolorallocate($ handle,0,255,0);
$blue = imagecolorallocate($handle, 0, 0, 255);
$ blue = imagecolorallocate($ handle,0,0,255);
$darkred = imagecolorallocate($handle, 150, 0, 0);
$ darkred = imagecolorallocate($ handle,150,0,0);
$darkblue = imagecolorallocate($handle, 0, 0, 150);
$ darkblue = imagecolorallocate($ handle,0,0,150);
$darkgreen = imagecolorallocate($handle, 0, 150, 0);
$ darkgreen = imagecolorallocate($ handle,0,150,0);
// 3D look
// 3D外观
for ($i = 60; $i > 50; $i--)
为($ i = 60; $ i> 50; $ i--)
{
{
imagefilledarc($handle, 50, $i, 100, 50, 0, $one, $darkred, IMG_ARC_PIE);
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,$ one,$ slide,$ darkblue,IMG_ARC_PIE);
if ($slide = 360)
如果($ slide = 360)
{
{
}
}
else
其他
{
{
imagefilledarc($handle, 50, $i, 100, 50, $slide, 360 , $darkgreen, 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,0,$ one,$ red,IMG_ARC_PIE);
imagefilledarc($handle, 50, 50, 100, 50, $one, $slide , $blue, IMG_ARC_PIE);
imagefilledarc($ handle,50,50,100,50,$ one,$ slide,$ blue,IMG_ARC_PIE);
if ($slide = 360)
如果($ slide = 360)
{
{
}
}
else
其他
{
{
imagefilledarc($handle, 50, 50, 100, 50, $slide, 360 , $green, IMG_ARC_PIE);
imagefilledarc($ handle,50、50、100、50,$ slide,360,$ green,IMG_ARC_PIE);
}
}
imagepng($handle);
imagepng($ handle);

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

在脚本中,您调用了vote_pie.php以显示结果的饼图。 上面的代码应放置在vote_pie.php文件中。 基本上,这是绘制圆弧来创建饼图。 您从主脚本的链接中传递了所需的变量。 为了更好地理解此代码,您应该阅读GD教程 ,其中涵盖了圆弧和圆弧。

This entire project can be downloaded from: http://github.com/Goatella/PHPGraphicalPoll

可以从以下网站下载整个项目: http : //github.com/Goatella/PHPGraphicalPoll

翻译自: https://www.thoughtco.com/simple-php-and-mysql-poll-2693854

php+mysql投票

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值