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

이전 장에서는 투표 표시 페이지의 각 모듈의 기능적 구현을 설명했습니다.
투표 기능을 선택하려면 클릭하세요
투표 결과 기능 보기
이 섹션에서는 이러한 기능을 색인에 완전히 표시합니다. 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"]?>" />
<?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>참고: 이 과정의 이 장에서는 다양한 기능을 간략하게 설명하며 해당 코드는 학습 참조용으로만 사용됩니다.
새로운 파일
<?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>
시사
Clear
- 코스 추천
- 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~
이 강좌를 시청한 학생들도 학습하고 있습니다.
PHP로 사업을 시작하는 방법에 대해 간단히 이야기해 보겠습니다.
웹 프론트 엔드 개발에 대한 빠른 소개
민망한 물건 백과사전 사이트를 모방한 Mini 버전 MVC 프레임워크의 대규모 실용 Tianlongbabu 개발
PHP 실용 개발 시작하기: 빠른 PHP 생성 [중소기업 포럼]
로그인 인증 및 클래식 게시판
컴퓨터 네트워크 지식 수집
빠른 시작 Node.JS 정식 버전
당신을 가장 잘 이해하는 프론트엔드 강좌: HTML5/CSS3/ES6/NPM/Vue/...[원본]
자신만의 PHP MVC 프레임워크 작성(깊이 있는 40개 장/자세한 내용/초보자가 발전하려면 읽어야 함)
















