投票系统

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

难点:

$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

?>

 


最后效果