一、题目要求:建立如下两个数据库,实现投票和%的统计结果:

二、具体编写方法:

(1)、建立数据库:

里面的蓝色背景的表格名称就是我们所需的表格!

表格内容如下:

表名:diaoyantimu

表名:diaoyanxuanxiang

(2)、封装类文件:

<?php
class DBDA
{
public $fuwuqi="localhost";
public $yonghuming="root";
public $mima="root"; public $dbconnect; function Query($sql,$type=1,$shujukuming="test")
{ $this->dbconnect = new MySQLi($this->fuwuqi,$this->yonghuming,$this->mima,$shujukuming); if(!mysqli_connect_error())
{ $result = $this->dbconnect->query($sql); if($type==1)
{ return $result->fetch_all();
}
else
{ return $result;
} }
else
{
return"连接失败"; } } } ?>

(3)、投票页面(shouye.php):

<!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 type="text/css">
.x
{
float:left;
}
#top
{
width:500px;
height:200px;
}
#bottom
{
width:500px;
height:200px;
display:none;
}
#fanhui
{
display:none;
}
</style> </head>
<body>
<form action="chuli.php" method="post">
<?php
include("DBDA.class.php");
$db=new DBDA;
$stm="select * from diaoyantimu limit 0,1";
$atm = $db->Query($stm); echo "<div>题目名称:{$atm[0][1]}</div>";
$sxx="select * from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'";
$axx=$db->Query($sxx); echo "<div id='top'>"; foreach($axx as $v)
{
echo "<div><input type='checkbox' value='{$v[0]}' name='xx[]'/>{$v[1]}</div>";
} echo "</div>"; ?> <div id="bottom">
<?php $sum="select sum(Numbers) from diaoyanxuanxiang where timudaihao='{$atm[0][0]}'";
$asum=$db->Query($sum);
$total=$asum[0][0]; foreach($axx as $v)
{
$bfb=($v[2]/$total)*100; echo "<div><span class='x'>{$v[1]}</span> <div class='x' style='border:1px solid blue;width:120px; height:12px'> <div style='background-color:red;height:12px;width:{$bfb}%'></div> </div> <span class='x'>&nbsp;{$v[2]}&nbsp;</span>
<span class='x'>{$bfb}%</span>
</div>
<div style='clear:both'></div>";
}
?> </div> <div id="anniu">
<input type="submit" value="提交"/>
<input type="button" value="查看" onclick="ShowResult()"/>
</div>
<div id="fanhui"><input type="button" value="返回" onclick="Show()" /></div> </form> </body> <script type="text/javascript">
function ShowResult()
{
document.getElementById("top").style.display="none";
document.getElementById("bottom").style.display="block";
document.getElementById("fanhui").style.display="block";
document.getElementById("anniu").style.display="none";
}
function Show()
{
document.getElementById("top").style.display="block";
document.getElementById("bottom").style.display="none";
document.getElementById("fanhui").style.display="none";
document.getElementById("anniu").style.display="block";
} </script> </html>

(4)、投票处理页面(chuli.php):

<?php
$ids = $_POST["xx"];
include("DBDA.class.php");
$db = new DBDA(); foreach($ids as $v)
{
$sql = "update diaoyanxuanxiang set Numbers = Numbers+1 where Ids='{$v}'";
$db->Query($sql,0);
} header("location:shouye.php");

显示结果如下:

php投票练习的更多相关文章

  1. 运用php做投票题,例题

    要求大概是这样的,有一个题目,题目下面是复选框,要求点完复选框提交后会变成进度条,各选项的进度条百分比,和投票数量 首先还是要在数据库建两张表,如下: 要完成这个题目,需要建两个页面 <!DOC ...

  2. easy_UI 投票列表

    首先我们考虑一下在项目投票种用到的属性(ID,投票标题,备选项目,参与人数) entity package cn.entity; public class GridNode { private Lon ...

  3. 利用django创建一个投票网站(六)

    建你的第一个 Django 项目, 第六部分 这一篇从第五部分(zh)结尾的地方继续讲起.再上一节中我们为网络投票程序编写了测试,而现在我们要为它加上样式和图片. 除了服务端生成的 HTML 以外,网 ...

  4. 利用django创建一个投票网站(五)

    创建你的第一个 Django 项目, 第五部分 这一篇从第四部分(en)结尾的地方继续讲起.我们在前几章成功的构建了一个在线投票应用,在这一部分里我们将其创建一些自动化测试. 自动化测试简介 自动化测 ...

  5. 利用django创建一个投票网站(四)

    创建你的第一个 Django 项目, 第四部分 这一篇从第三部分(zh)结尾的地方继续讲起.我们将继续编写投票应用,专注于简单的表单处理并且精简我们的代码. 编写一个简单的表单 让我们更新一下在上一个 ...

  6. 利用django创建一个投票网站(三)

    创建你的第一个 Django 项目, 第三部分 这一篇从第二部分(zh)结尾的地方继续讲起.我们将继续编写投票应用,并且聚焦于如何创建公用界面--也被称为"视图". 设计哲学 Dj ...

  7. 利用django创建一个投票网站(二)

    创建你的第一个 Django 项目, 第二部分 这一篇从第一部分(zh)结尾的地方继续讲起.本节我们将继续写 Web 投票应用,并主要关注 Django 提供的自动生成的管理页面(admin site ...

  8. 利用django创建一个投票网站(一)

    这是教程的原始链接:http://django-intro-zh.readthedocs.io/zh_CN/latest/part1/ 创建你的第一个 Django 项目, 第一部分 来跟着实际项目学 ...

  9. Django基础,Day5 - form表单投票详解

    投票URL polls/urls.py: # ex: /polls/5/vote/ url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, ...

  10. PHP类的封装和做投票和用进度条显示

    三处理传过来的数据1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

随机推荐

  1. 3)Javascript设计模式:Observer模式

    Observer模式 var Observer = (function() { var instance = null; function Observe() { this.events = {} } ...

  2. flash跨域访问

    -------------------------------------------------------背景------------------------------------------- ...

  3. dev中TreeList的应用(转)

    如果需要在单元格添加时则用TreeList如果只是单纯读取数据或检索数据时则用GridControl 1.如果点击添加 时则添加TreeList的节点: protected internal void ...

  4. ExtJS与后台Java交互

    参考博客:http://blog.csdn.net/wanghuan203/article/details/8125970 开发环境:Eclipse + Tomcat + ExtJS6.0 工程目录结 ...

  5. 玩转微信小程序

    原文链接 2007 年 1 月 9 号,苹果一代在功能机盛行的年代中出世. 2017 年 1 月 9 号,微信小程序在重型app风靡的压力下上线. 苹果的出世掀起了互联网一波又一波的浪潮,而微信小程序 ...

  6. 实现过程全纪录——自己写一个“微信朋友圈”(包括移动端与PC端)

    一.朋友圈的基本单元--动态 首先定义一个自定义控件用来显示每条动态. 二.运行效果 三.核心解读 PushedMessage 有个PushIndex属性,表示发送消息的index,从0开始递增.每个 ...

  7. log4jdbc打印完整SQL

    一.log4jdbc简单介绍: log4jdbc是工作在jdbc层的一个日志框架,能够记录SQL及数据库连接执行信息. 一般的SQL日志会把占位符和参数值分开打印,log4jdbc则会记录数据库执行的 ...

  8. [转载] HTTP协议详解

    转自:http://blog.csdn.net/gueter/archive/2007/03/08/1524447.aspx Author :Jeffrey 引言 HTTP是一个属于应用层的面向对象的 ...

  9. Java虚拟机(JVM)默认字符集详解

    Java中对字符串等进行转换字节数组时, 需要根据字符集编码来进行转换, 当不显示的指定字符集编码时(如: "测试".getBytes()), 会使用Charset.default ...

  10. linux pagecache与内存占用

    实验环境 CentOS Linux release 7.3.1611 (Core)  3.10.0-514.6.1.el7.x86_64   一.概念介绍      linux系统中通常使用free命 ...