码迷,mamicode.com
首页 > 其他好文 > 详细

投票系统

时间:2018-06-20 00:55:00      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:tle   com   fetch   进度   规划   i++   选项   分享图片   ade   

制作一个投票系统   在查看详情 能看到投票总票数与各个选项之间的关系:

难点:

$conn = new mysqli("localhost","root","","ceshi");
if($conn->connect_error){
die("连接失败");
}
$sql_sum = "select sum(number) from toupiao";
$result_sum = $conn->query($sql_sum);
$attr_sum = $result_sum->fetch_row();
$sql = "select id,number from toupiao";
$result = $conn->query($sql);
$attr = $result->fetch_all();
$str = "";
for($i=0;$i<count($attr);$i++){
$bfb = $attr[$i][1]/$attr_sum[0]*100;
$str.="<div><span>{$attr[$i][0]}</span><span>{$bfb}%</span></div> <div class=‘a‘><div class=‘b‘ style=‘width:{$bfb}%‘></div></div>";
}
echo $str;
?>

技术分享图片

下面是主页:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>投票</title>
</head>
<body>
    你35岁前的职业规划是什么?
    <form action="chuli.php    " method="post">
        <input type="checkbox" name="tp[]"  value="1" />aaaaa<br />
        <input type="checkbox" name="tp[]"  value="2" />bbbbb<br />
        <input type="checkbox" name="tp[]"  value="3" />ccccc<br />
        <input type="checkbox" name="tp[]"  value="4" />ddddd<br />
        <input type="checkbox" name="tp[]"  value="5" />fffff<br />
        <input type="submit" value="投票"/><br />
             <a href="jindutiao.php">查看详情</a>
    </form>
</body>
</html>

接下来是php简单处理页面

<?php
    $tp=$_POST[tp];
    $str=join("‘,‘",$tp);
    $diyi = "localhost";
    $user = "root";
    $disanbu = "";
    $disibuname = "ceshi";
    $conn = new mysqli($diyi,$user,$disanbu,$disibuname);
    if($conn->connect_error){
        die("连接失败");
    }
    $sql = "update toupiao set number = number+1 where id in(‘{$str}‘)";
    if($result = $conn->query($sql)){
    header("location:toupiao.php");
}

?>
在接下来 就是 查看详情显示 进度条样式的总占比:
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <style>
        .a{
            width: 100px;
            height: 10px;
            border: 1px solid black;
        }
        .b{
            height: 100%;
            background-color: red;
        }
        
    </style>
</head>
<body>
    <?php
        $conn = new mysqli("localhost","root","","ceshi");
        if($conn->connect_error){
        die("连接失败");
        }
        $sql_sum = "select sum(number) from toupiao";
        $result_sum = $conn->query($sql_sum);
        $attr_sum = $result_sum->fetch_row();
        $sql = "select id,number from toupiao";
        $result = $conn->query($sql);
        $attr = $result->fetch_all();
        $str = "";
        for($i=0;$i<count($attr);$i++){
        $bfb = $attr[$i][1]/$attr_sum[0]*100;
        $str.="<div><span>{$attr[$i][0]}</span><span>{$bfb}%</span></div> <div class=‘a‘><div class=‘b‘ style=‘width:{$bfb}%‘></div></div>";
        }
        echo $str;
?>
    <a href="toupiao.php">返回</a>
</body>
</html><?php

?>

 


技术分享图片
最后效果

投票系统

标签:tle   com   fetch   进度   规划   i++   选项   分享图片   ade   

原文地址:https://www.cnblogs.com/zhengleilei/p/9201761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!