练习题目:

解题:

方法一:

1. 投票主页面:

<!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=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">
<?php
include ("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.处理投票页面:

<?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. 建立访问数据库的类,封装用于引用:

<?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. 投票主页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<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">
<?php
include ("../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. 处理投票页面:

<?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. 查看投票结果页面:

<!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=utf-8" />
<title>查看结果</title>
<style>
.x
{
float:left;}
</style>
</head>
<body> <?php
include ("../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 之 查询 投票练习(0508)的更多相关文章

  1. 投票系统开发总结struts2,jfreechart,cookie应用,以及前端技术

    struts2配置web.xml+struts.xml: <?xml version="1.0" encoding="UTF-8"?> <we ...

  2. JavaWeb项目开发案例精粹-第2章投票系统-004action层

    1. package com.sanqing.action; import java.util.UUID; import com.opensymphony.xwork2.ActionSupport; ...

  3. JavaWeb项目开发案例精粹-第2章投票系统-003Dao层

    1. package com.sanqing.dao; import java.util.List; import com.sanqing.bean.Vote; import com.sanqing. ...

  4. Django写的投票系统2(转)

    在上一篇中 django实例:创建你的第一个应用投票系统(一) 已经介绍基本的功能,并已经启动服务了.这一节介绍数据库相关的东东. 首页打开mysite/settings.py配置文件, 设置数据库打 ...

  5. php......调研投票练习

    调研题目与调研选项显示页面<style type="text/css"> #list{ width:400px; height:200px;} #jieguo{ wid ...

  6. 第二节:模型(Models)和管理后台(Admin site)

    本节内容我们将配置数据库,创建第一个model并且快速了解Django自动生成的管理后台(admin site) 目录 数据库配置 创建模型 激活模型 使用Django API 介绍Django管理后 ...

  7. Windows Azure案例分析: 选择虚拟机或云服务?

    作者 王枫 发布于2013年6月27日 随着云计算技术和市场的日渐成熟,企业在考虑IT管理和运维时的选择也更加多样化,应用也从传统部署方式,发展为私有云.公有云.和混合云等部署方式.作为微软核心的公有 ...

  8. python is 和 == 的区别

    一.is 和 == 的区别 == 比较 比较的俩边的值 is 比较 比较的是内存地址 id() 二.小数据池 数字小数据池的范围 -5 ~ 256 字符串中如果有特殊字符他们的内存地址就不一样 字符串 ...

  9. phpcms前台任意代码执行漏洞(php<5.3)

    phpcms v9 中 string2array()函数使用了eval函数,在多个地方可能造成代码执行漏洞 /phpsso_server/phpcms/libs/functions/global.fu ...

随机推荐

  1. 爬虫技术浅析 | z7y Blog

    爬虫技术浅析 | z7y Blog 爬虫技术浅析

  2. 364. Nested List Weight Sum II

    这个题做了一个多小时,好傻逼. 显而易见计算的话必须知道当前层是第几层,因为要乘权重,想要知道是第几层又必须知道最高是几层.. 用了好久是因为想ONE PASS,尝试过遍历的时候构建STACK,通过和 ...

  3. SQL server 创建表,索引,主键,外键

    if object_id('student', 'U') is not null drop table student go create table student( sno varchar(20) ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码]

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(6)-Unity 2.x依赖注入by运行时注入[附源码] Unity 2.x依赖注入(控制反转)IOC,对 ...

  5. linux下切割catalina.out文件,按天生成文件

    1.下载工具cronolog压缩包(http://download.csdn.net/detail/sunling_sz/8144469) 2.将文件拖放到server,不论什么文件夹都能够. 3.进 ...

  6. LeetCode201 Bitwise AND of Numbers Range Java 题解

    题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...

  7. CSS——(1)基础

    CSS(Cascading Style Sheets)层叠样式表 含义 一种计算机语言: 能够实现网页与内容分离: 用来表现文件样式,如HTML或XML: 比較 div与css 假设说div是容器的话 ...

  8. [RxJS] Combination operator: withLatestFrom

    Operator combineLatest is not the only AND-style combinator. In this lesson we will explore withLate ...

  9. nginx代理人server结合tomcat采用

    相信非常多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那到底它有什么作用呢?可能非常多人未必了解. 说到反向代理,可能非常多人都听说,但详细什么是反向代理,非常多人预计就 ...

  10. hadoop小文件合并

    1.背景 在实际项目中,输入数据往往是由许多小文件组成,这里的小文件是指小于HDFS系统Block大小的文件(默认128M), 然而每一个存储在HDFS中的文件.目录和块都映射为一个对象,存储在Nam ...