완전한 투표 표시 기능 모듈을 갖춘 PHP로 개발된 간단한 투표 시스템

116.png

이전 장에서는 투표 표시 페이지의 각 모듈의 기능적 구현을 ​​설명했습니다.

투표 기능을 선택하려면 클릭하세요

투표 결과 기능 보기

이 섹션에서는 이러한 기능을 색인에 완전히 표시합니다. php file

<?php
ob_start();
session_start();
header("Content-type:text/html;charset=utf-8");

$link = mysqli_connect('localhost','username','password','test');
mysqli_set_charset($link, "utf8");
if (!$link) {
  die("连接失败:".mysqli_connect_error());
}

?>
<!doctype html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>投票系统</title>
  <script type="text/javascript" src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
  <style type="text/css">
    /*全局样式*/
    body { font-family: "宋体"; font-size: 12pt; color: #333333; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;background-color: #A6C7E1;}
    table { font-family: "宋体"; font-size: 9pt; line-height: 20px; color: #333333}
    a:link { font-size: 9pt; color: #333333; text-decoration: none}
    a:visited { font-size: 9pt; color: #333333; text-decoration: none}
    a:hover { font-size: 9pt; color: #E7005C; text-decoration: underline}
    a:active { font-size: 9pt; color: #333333; text-decoration: none}
    /*全局样式结束*/
  </style>
  <script language="javascript">
    function check()
    {
      node=frm.itm;
      flag=false;
      for(i=0;i<node.length;i++)
      {
        if(node[i].checked)
        {
          flag=true;
        }
      }
      if(!flag)
      {
        alert("您没有选择")
        return false;
      }
      return true;
    }
  </script>

  <?php

  if(isset($_POST["submit"])){

    if($_POST){
      $id = $_POST["selected_id"];
      $sql = "update vote set count = count+1 where id=$id";
      mysqli_query($link,$sql);

    }

    if($_SESSION["vote"]==session_id())
    {
      ?>
      <script language="javascript">
        alert("您已经投票了");
        location.href="index.php";
      </script>
    <?php
    exit();
    }
    $id=$_POST["itm"];
    $sql="update vote set count=count+1 where id=$id";
    if(mysqli_query($link,$sql))
    {
    $_SESSION["vote"]=session_id();
    ?>
      <script language="javascript">alert("投票成功,点确定查看结果");location.href="index.php?id=ck";</script>
    <?php
    }
    else
    {
    ?>
      <script language="javascript">alert("投票失败");location.href="index.php";</script>
      <?php
    }
  }
  ?>

</head>
<body>
<form name="frm" action="" method="post" onsubmit=return(check()) style="margin-bottom:5px;">
  <table width="365" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#C2C2C2">
    <tr>
      <th bgcolor="#FFFFCC">
        <?php
        $sql="select * from votetitle";
        $rs=mysqli_query($link,$sql);
        $row=mysqli_fetch_assoc($rs);
        echo $row["votetitle"];
        ?> </th>
    </tr>
    <?php
    $sql="select * from vote";
    $rs=mysqli_query($link,$sql);
    while($rows=mysqli_fetch_assoc($rs))
    {
      ?>
      <tr>
        <td bgcolor="#FFFFFF"><input type="radio" name="itm" value="<?php echo $rows["id"]?>" />&nbsp;&nbsp;
          <?php echo $rows["item"]?></td>
      </tr>
      <?php
    }
    ?>
    <tr>
      <td align="center" bgcolor="#FFFFFF">
        <input type="submit" name="submit" value="投票"/>
        <input type="hidden" id="selected_id" name="selected_id" value="">
        <input type="button" value="查看结果" onClick="location.href='index.php?id=ck'"/>&nbsp;&nbsp;
        <script type="text/javascript">
            $("[type='radio']").click(function(){
               $("#selected_id").val($(this).val());
            });
        </script></td>
    </tr>
  </table>
</form>
<?php

if(isset($_GET["id"])=="ck"){?>
  <?php

  $sql="select sum(count) as 'total' from vote";
  $rs=mysqli_query($link,$sql);
  $rows=mysqli_fetch_assoc($rs);
  $sum=$rows["total"];  //得出总票数

  $sql="select * from vote";
  $rs=mysqli_query($link,$sql);
  ?>
  <table id="click" width="365" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#C2C2C2" >
    <tr>
      <th bgcolor="#FFFFFF">项目</th>
      <th bgcolor="#FFFFFF">票数</th>
      <th bgcolor="#FFFFFF">百分比</th>
    </tr>
    <?php
    while($rows=mysqli_fetch_assoc($rs))
    {
      ?>
      <tr>
        <td bgcolor="#FFFFFF"><?php echo $rows["item"]?></td>
        <td bgcolor="#FFFFFF"><?php echo $rows["count"]?></td>
        <td bgcolor="#FFFFFF">
          <?php
          $per=$rows["count"]/$sum;
          $per=number_format($per,4);
          ?>
          <img src="" height="4" width="<?php echo $per*100?>" />
          <?php echo $per*100?>%      </td>
      </tr>
      <?php
    }
    ?>
  </table>
  <div align="center">
    <a href="index.php">隐藏结果</a>
  </div>
<?php } ?>
</body>
</html>

참고: 이 과정의 이 장에서는 다양한 기능을 간략하게 설명하며 해당 코드는 학습 참조용으로만 사용됩니다.


지속적인 학습
||
<?php ob_start(); session_start(); header("Content-type:text/html;charset=utf-8"); $link = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($link, "utf8"); if (!$link) { die("连接失败:".mysqli_connect_error()); } ?> <!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>投票系统</title> <script type="text/javascript" src="//cdn.bootcss.com/jquery/1.12.4/jquery.js"></script> <style type="text/css"> /*全局样式*/ body { font-family: "宋体"; font-size: 12pt; color: #333333; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px;background-color: #A6C7E1;} table { font-family: "宋体"; font-size: 9pt; line-height: 20px; color: #333333} a:link { font-size: 9pt; color: #333333; text-decoration: none} a:visited { font-size: 9pt; color: #333333; text-decoration: none} a:hover { font-size: 9pt; color: #E7005C; text-decoration: underline} a:active { font-size: 9pt; color: #333333; text-decoration: none} /*全局样式结束*/ </style> <script language="javascript"> function check() { node=frm.itm; flag=false; for(i=0;i<node.length;i++) { if(node[i].checked) { flag=true; } } if(!flag) { alert("您没有选择") return false; } return true; } </script> <?php if(isset($_POST["submit"])){ if($_POST){ $id = $_POST["selected_id"]; $sql = "update vote set count = count+1 where id=$id"; mysqli_query($link,$sql); } if($_SESSION["vote"]==session_id()) { ?> <script language="javascript"> alert("您已经投票了"); location.href="index.php"; </script> <?php exit(); } $id=$_POST["itm"]; $sql="update vote set count=count+1 where id=$id"; if(mysqli_query($link,$sql)) { $_SESSION["vote"]=session_id(); ?> <script language="javascript">alert("投票成功,点确定查看结果");location.href="index.php?id=ck";</script> <?php } else { ?> <script language="javascript">alert("投票失败");location.href="index.php";</script> <?php } } ?> </head> <body> <form name="frm" action="" method="post" onsubmit=return(check()) style="margin-bottom:5px;"> <table width="365" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#C2C2C2"> <tr> <th bgcolor="#FFFFCC"> <?php $sql="select * from votetitle"; $rs=mysqli_query($link,$sql); $row=mysqli_fetch_assoc($rs); echo $row["votetitle"]; ?> </th> </tr> <?php $sql="select * from vote"; $rs=mysqli_query($link,$sql); while($rows=mysqli_fetch_assoc($rs)) { ?> <tr> <td bgcolor="#FFFFFF"><input type="radio" name="itm" value="<?php echo $rows["id"]?>" />   <?php echo $rows["item"]?></td> </tr> <?php } ?> <tr> <td align="center" bgcolor="#FFFFFF"> <input type="submit" name="submit" value="投票"/> <input type="hidden" id="selected_id" name="selected_id" value=""> <input type="button" value="查看结果" onClick="location.href='index.php?id=ck'"/>   <script type="text/javascript"> $("[type='radio']").click(function(){ $("#selected_id").val($(this).val()); }); </script></td> </tr> </table> </form> <?php if(isset($_GET["id"])=="ck"){?> <?php $sql="select sum(count) as 'total' from vote"; $rs=mysqli_query($link,$sql); $rows=mysqli_fetch_assoc($rs); $sum=$rows["total"]; //得出总票数 $sql="select * from vote"; $rs=mysqli_query($link,$sql); ?> <table id="click" width="365" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#C2C2C2" > <tr> <th bgcolor="#FFFFFF">项目</th> <th bgcolor="#FFFFFF">票数</th> <th bgcolor="#FFFFFF">百分比</th> </tr> <?php while($rows=mysqli_fetch_assoc($rs)) { ?> <tr> <td bgcolor="#FFFFFF"><?php echo $rows["item"]?></td> <td bgcolor="#FFFFFF"><?php echo $rows["count"]?></td> <td bgcolor="#FFFFFF"> <?php $per=$rows["count"]/$sum; $per=number_format($per,4); ?> <img src="" height="4" width="<?php echo $per*100?>" /> <?php echo $per*100?>% </td> </tr> <?php } ?> </table> <div align="center"> <a href="index.php">隐藏结果</a> </div> <?php } ?> </body> </html>
  • 코스 추천
  • 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~