练习------投票

一 、题目要求:

二 、做法

【1】建立数据库

表名:diaoyantimu

表名: diaoyanxuanxiang

【2】封装类文件

 <?php
class DBDA
{
public $fuwuqi="localhost"; //服务器地址
public $yonghuming="root";//用户名
public $mima="";//密码 public $dbconnect;//连接对象 //操作数据库的方法 //$sql代表需要执行的SQL语句
//$type代表SQL语句的类型,1代表查询,2代表增删改
//$shujukuming代表数据库的名称
//如果是查询,返回二维数组
//如果是增删改,返回true或false function Query($sql,$type=1,$shujukuming="toupiao")
{
//造连接对象
$this->dbconnect = new MySQLi($this->fuwuqi,$this->yonghuming,$this->mima,$shujukuming); //判断是否出错
if(!mysqli_connect_error())
{
//如果连接成功,执行SQL语句
$result = $this->dbconnect->query($sql); //根据语句类型判断
if($type==1)
{
//如果是查询语句,返回二维数组
return $result->fetch_all();
}
else
{
//如果是其他语句,返回true或false
return $result;
} }
else
{
return"连接失败"; } } } ?>

【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 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)//$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;//$total 提取的是数据库里的总值 所以数据库要有数Numbers 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> <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"]; //要接收传过来的值 谁传的? name='xx[]
include("DBDA.class.php");
$db = new DBDA(); foreach($ids as $v)//$v就是主键值
{
$sql = "update diaoyanxuanxiang set Numbers = Numbers+1 where Ids='{$v}'";
$db->Query($sql,0);//不是查询语句,所以是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. 【Bigdecimal】

    ---恢复内容开始--- 大位数除法的时候注意1/3问题:异常:[Exception in thread "main" java.lang.ArithmeticException: ...

  2. MvvmLight - ViewModelLocator

    这里先鼓舞下士气,ViewModelLocator很简单,甚至可以去掉,它不是Mvvm必须的.在初学Mvvm时,一般都是使用NuGet安装 MvvmLight框架,总是会带上那么一个ViewModel ...

  3. Tool Scripts

    1. Function: 16进制转字符串 Create FUNCTION [dbo].[f_hextostr] (@hexstring VARCHAR(max)) RETURNS VARCHAR(m ...

  4. 记一次java程序内存溢出问题

    一个自然语言处理程序,在封装为web-service后,部署到线上运行. 但最近出现了内存溢出的情况,频繁的out of memory. 先盲目尝试在启动脚本中增加-XX:-UseGCOverhead ...

  5. SQL update 多表连接方法

    SQL Update多表联合更新的方法 () sqlite 多表更新方法 //---------------------------------- update t1 set col1=t2.col1 ...

  6. mybatis学习之高级映射

    一对一映射查询 1.数据库执行脚本: /* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.7.11-log : Database - db_mybatis *** ...

  7. equals()重写

    ** 注意 ** 1.重写equals方法修饰符必须是public,因为是重写的Object的方法. 2.参数类型必须是Object. 3.重写equals方法后最好重写hashCode方法,否则两个 ...

  8. UVA1339(字母映射)

    memcmp(const void *buf1, const void *buf2, unsigned int count)可以比较两个串相等 http://baike.baidu.com/link? ...

  9. hdu1385 最短路字典序

    http://blog.csdn.net/ice_crazy/article/details/7785111 http://blog.csdn.net/shuangde800/article/deta ...

  10. Codeforces183D T-shirt

    传送门 这题好神啊……(然而我连每种物品贡献独立都没看出来…… 首先$O(n^2 m)$的DP肯定都会写,然后可以发现每种物品一定是选得越多再选一个的收益就越低,因此可以用一个堆维护当前收益最高的物品 ...