php查询操作实现投票功能
这篇文章主要为大家详细介绍了php查询操作实现投票功能的具体代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了php查询操作实现投票功能的代码,供大家参考,具体内容如下
题目:



解题方法汇总:
方法一:
1. 投票主页面:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
 | 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>投票</title><style>#list{  width:400px;  height:300px;}#jieguo{  width:400px;  height:300px;}.x{      float:left;}  </style></head>  <body>  <form action="TouChuLi.php" method="post"><?phpinclude ("DBDA.class.php");$db=new DBDA();//从调研题目表中找出题目代号和名称$sql="select * from diaoyantimu limit 0,1";$arr=$db->query($sql);$tmmc=$arr[0][1];$tmdh=$arr[0][0];echo "<div><h2>{$tmmc}:</h2></div>";//从调研选项表中输出选项内容:$sqlxx="select * from diaoyanxuanxiang where timudaihao='{$tmdh}'";$arrxx=$db->query($sqlxx);echo "<div id='list'>";foreach ($arrxx as $v){  echo "<div><input type='checkbox' value='{$v[0]}' name='xx[]'>{$v[1]}</div><br />";}  ?><input type="submit" value="提交"><input type="button" value="查看结果" id="check" onclick="Showjieguo()"></form></div>  <div id="jieguo" style="display:none"><?php//计算总人数:$sqlzs="select sum(numbers) from diaoyanxuanxiang where timudaihao='{$tmdh}'";$zrs=$db->query($sqlzs);  foreach ($arrxx as $v){      $name=$v[1];  $number=$v[2];  if($zrs[0][0]==0)   {     $bfb = 0;   }   else   {     $bfb = ($number/$zrs[0][0])*100;   }  $bfb=round($bfb,2);  echo "<div>    <span class='x'>{$name} </span>    <div class='x' style='width:200px; height:10px; background-color:#808080'>    <div style='width:{$bfb}%; height:10px; background-color:#FF8040'> </div>    </div>    <span class='x'>{$number} </span>    <span class='x'>{$bfb}% </span>    </div>    <br />    "; }?><input type="button" value="返回" id="fanhui" onclick="Showfanhui()"></div><script>function Showjieguo(){  document.getElementById("list").style.display="none";  document.getElementById("jieguo").style.display="block";  }function Showfanhui(){  document.getElementById("list").style.display="block";  document.getElementById("jieguo").style.display="none";  }</script>  </body></html> | 
2.处理投票页面:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
<?php$arr=$_POST["xx"];include ("../DBDA.class.php");$db=new DBDA();    foreach($arr as $v){  $sql = "update diaoyanxuanxiang set numbers = numbers+1 where ids = '{$v}'";  $db->Query($sql,1);//1代表$sql的类型}header ("location:TouPiao.php");?> | 
3. 建立访问数据库的类,封装用于引用:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
 | 
<?php//执行一个sql语句,返回相应的结果class DBDA{  public $host="localhost";//数据库服务器地址  public $uid="root";//数据库用户名  public $password="";//数据库密码  //执行SQL语句的方法  //参数里面:$sql代表要执行的sql语句;$type是sql语句的类型,0代表查询,1代表其他(增删改);$db代表要操作的数据库  function Query($sql,$type=0,$db="mydb")  {    //造连接对象    $dbconnect=new MySQLi($this->host,$this->uid,$this->password,$db);    //判断连接是否出错    !mysqli_connect_error() or die("连接失败!");    //执行sql语句    $result=$dbconnect->query($sql);    //判断SQL语句类型    if($type==0)    {       //如果是查询语句返回结果集的二维数组      return $result->fetch_all();      }    else    {      //如果是其他语句,返回true或false      return $result;      }       }        } | 
方法二:
1. 投票主页面:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
 | 
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>投票</title><style>.x{      float:left;}  </style></head>  <body>  <form action="TouChuLi.php" method="post"><?phpinclude ("../DBDA.class.php");$db=new DBDA();//从调研题目表中找出题目代号和名称$sql="select * from diaoyantimu limit 0,1";$arr=$db->query($sql);$tmmc=$arr[0][1];$tmdh=$arr[0][0];echo "<div><h2>{$tmmc}:</h2></div>";//从调研选项表中输出选项内容:$sqlxx="select * from diaoyanxuanxiang where timudaihao='{$tmdh}'";$arrxx=$db->query($sqlxx);echo "<div id='list'>";foreach ($arrxx as $v){  echo "<div><input type='checkbox' value='{$v[0]}' name='xx[]'>{$v[1]}</div><br />";}  ?><input type="submit" value="提交"><a href="ChaKan.php"><input type="button" value="查看结果" id="check" ></a></form></body></html> | 
2. 处理投票页面:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
<?php$arr=$_POST["xx"];include ("../DBDA.class.php");$db=new DBDA();    foreach($arr as $v){  $sql = "update diaoyanxuanxiang set numbers = numbers+1 where ids = '{$v}'";  $db->Query($sql,1);//1代表$sql的类型}header ("location:TouPiao.php");?> | 
3. 查看投票结果页面:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
 | 
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>查看结果</title><style>.x{  float:left;}</style></head><body>  <?phpinclude ("../DBDA.class.php");$db=new DBDA();//从调研题目表中找出题目代号和名称$sql="select * from diaoyantimu limit 0,1";$arr=$db->query($sql);$tmmc=$arr[0][1];echo "<div><h2>{$tmmc}:</h2></div>";//从调研选项表中输出选项内容:$sqlxx="select * from diaoyanxuanxiang where timudaihao='{$arr[0][0]}'";$arrxx=$db->query($sqlxx);  //计算总人数:$sqlzs="select sum(numbers) from diaoyanxuanxiang where timudaihao='{$arr[0][0]}'";$zrs=$db->query($sqlzs);  foreach ($arrxx as $v){   $name=$v[1];//调研项目名称   $number=$v[2];//选择该项的人数   //判断总人数是否为0   if($zrs[0][0]==0)   {     $bfb = 0;   }   else   {     $bfb = ($number/$zrs[0][0])*100;//求百分比   }      $bfb=round($bfb,2); //取小数点后两位  echo "<div>  <span class='x'>{$name} </span>  <div class='x' style='width:200px; height:10px; background-color:#808080'>  <div style='width:{$bfb}%; height:10px; background-color:#FF8040'> </div>  </div>  <span class='x'>{$number} </span>  <span class='x'>{$bfb}%</span><br />  </div><br />"; }?><br /><a href="TouPiao.php"><input type="button" value="返回"></a></body></html> | 
显示结果:


以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。
php查询操作实现投票功能的更多相关文章
- C#3.0新增功能09 LINQ 基础04 基本 LINQ 查询操作
		
连载目录 [已更新最新开发文章,点击查看详细] 本篇介绍 LINQ 查询表达式和一些在查询中执行的典型操作. 获取数据源 在 LINQ 查询中,第一步是指定数据源. 和大多数编程语言相同,在使用 ...
 - [C#] 进阶 - LINQ 标准查询操作概述
		
LINQ 标准查询操作概述 序 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> ...
 - MongoDB的查询操作
		
1. 前言 在这篇博文中,我们将学习如何查询mongoDB中的数据.当我们把数据存储在mongoDB以后,我们需要把数据查询出来.毕竟CRUD操作中,查询操作在我们系统中是我们应用比较频繁的操作.我们 ...
 - Django 1.10 中文文档------3.2.2 查询操作making queries
		
3.2.2 查询操作 6.15章节包含所有模型相关的API解释. 后面的内容基于如下的一个博客应用模型: from django.db import models class Blog(models. ...
 - .NET LINQ基本查询操作
		
获取数据源 在 LINQ 查询中,第一步是指定数据源.像在大多数编程语言中一样,在 C# 中,必须先声明变量,才能使用它.在 LINQ 查询中,最先使用 from 子句的目的是引入数据源 ( ...
 - Linq查询操作之Where筛选
		
筛选操作where能够处理逻辑运算符组成的逻辑表达式.比如逻辑“与”,逻辑“或”,并从数据源中筛选数据,它和where子句的功能非常相似.Enumerable类的Where()原型如下: public ...
 - OracleHelper(对增删改查分页查询操作进行了面向对象的封装,对批量增删改操作的事务封装)
		
公司的一个新项目使用ASP.NET MVC开发,经理让我写个OracleHelper,我从网上找了一个比较全的OracleHelper类,缺点是查询的时候返回DataSet,数据增删改要写很多代码(当 ...
 - MongoDB源码分析——mongod数据查询操作
		
源码版本为MongoDB 2.6分支 Edit mongod数据查询操作 在mongod的初始化过程中说过,服务端接收到客户端消息后调用MyMessageHandler::process函数处理消息. ...
 - 查询操作  -- Django从入门到精通系列教程
		
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...
 
随机推荐
- C++ - 动态申请数组空间
			
// 用指针p指向由new动态分配的长度为length*sizeof(int)的内存空间. int * p = new int[length];
 - sql数据类型总结
			
一.数字数据类型 bigint int smallint tinyint decimal numeric money smallmoney float real Bit 二.字符数据类型 非unico ...
 - 使用System.IO.Combine(string path1, string path2, string path3)四个参数的重载函数提示`System.IO.Path.Combine(string, string, string, string)' is inaccessible due to its protection level
			
今天用Unity5.5.1开发提取Assets目录的模块,使用时采用System.IO.Path.Combine(string, string, string, string)函数进行路径生成 明明是 ...
 - linux内核阻塞IO
			
阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后再进行操作.被挂起的进程进入休眠状态,被从调度器的运行队列移走,知道等待的条件被满足.而非阻塞的进程在不能进行设备操作时, ...
 - redis的window客户端下载地址
			
这里是window的版本,由微软维护的: https://github.com/MicrosoftArchive/redis/releases
 - 使用explain分析sql语句
			
sql语句优化 : sql语句的时间花在哪儿? 答: 等待时间 , 执行时间. 这两个时间并非孤立的, 如果单条语句执行的快了,对其他语句的锁定的也就少了. 所以,我们来分析如何降低执行时间. : s ...
 - 6款强大的 jQuery 网页布局创建及优化插件
			
本文将为您介绍6款功能强大的jQuery插件,它们能够帮助您方便快捷地创建复杂的网络布局并进行优化. 1.UI.Layout 该插件可以创建任何你想要的UI形式:包括从简单的标题或侧边栏,到一个包含工 ...
 - LVS学习笔记及总结(思维导图版)
			
转自: http://www.07net01.com/2015/10/944377.html 下图是我在跟随马哥的脚步学习LVS过程中的学习笔记,以此为蓝本总结的,若有不足之处请谅解!
 - 那些有关求解next数组的算法
			
next数组的历史 有关字符串的模式匹配算法中,比较容易写出的是朴素的匹配算法也就是一种暴力求解方式,但是由于其时间复杂度为子串长度和主串长度的乘积,例如strlen(subStr) = n,strl ...
 - c++ json cpp
			
一 编译链接 1 在相应官网下载jsoncpp 2 解压得到jsoncpp-src-0.5.0文件 3 打开jsoncpp-src-0.5.0 -> makefiles -> vs71 - ...