一個(gè)容易的PHP投票系統(tǒng)
發(fā)表時(shí)間:2024-05-19 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]作者:轉(zhuǎn)接卡建立2個(gè)表,一個(gè)用來顯示當(dāng)前主題,一個(gè)存放歷史結(jié)果。 當(dāng)前主題表結(jié)構(gòu):選項(xiàng)(包括主題),票數(shù) 歷史結(jié)果表結(jié)構(gòu):id,主題名,關(guān)點(diǎn),投票開始時(shí)間,投票結(jié)束時(shí)間 管理頁面: 功能:1、更新投票主題 2、查看歷史結(jié)果 3、停止使用投票系統(tǒng) 一、建表 CREATE Table toupiaoa...
作者:轉(zhuǎn)接卡
建立2個(gè)表,一個(gè)用來顯示當(dāng)前主題,一個(gè)存放歷史結(jié)果。
當(dāng)前主題表結(jié)構(gòu):選項(xiàng)(包括主題),票數(shù)
歷史結(jié)果表結(jié)構(gòu):id,主題名,關(guān)點(diǎn),投票開始時(shí)間,投票結(jié)束時(shí)間
管理頁面:
功能:1、更新投票主題 2、查看歷史結(jié)果 3、停止使用投票系統(tǒng)
一、建表
CREATE Table toupiaoall(
id int(4) NOT NULL auto_increment,
theme char(20) NOT NULL,
idea char(100) NOT NULL,
begin char(20) NOT NULL,
end char(20) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE toupiaocur(
xx varchar(20) NOT NULL,
ps int(6) not null,
date char(10) not null
);
說明:在表toupiaocur中,第一個(gè)記錄存放主題和建立時(shí)間,在進(jìn)行投票時(shí)不改動(dòng)。從第二個(gè)記錄開始記錄各選項(xiàng)內(nèi)容和票數(shù)。
鏈接程序:connect.inc.php3
<?
$show="yes";
$xuanxiang=array("","one","two","thr","fou","fiv");
$current="toupiaocur";
$alldata="toupiaoall";
@mysql_connect("localhost","","") or die ("sorry,unable to connect to database");
@mysql_select_db("db") or die ("unable to select database");
?>
二、顯示頁面
<?
//頁面
include("connect.inc.php3");
$query_tp="select * from $current";
$result_tp=mysql_query($query_tp);
$row_tp=@mysql_fetch_array($result_tp);
$rows_tp=@mysql_num_rows($result_tp);
if ($rows_tp) {
?>
<hr size="1">
<script language="JavaScript">
<!--
function newin() {
var newwin=window.open("","homeWin","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=240,height=180");
return false;
}
//-->
</script>
<form name="fromtp" action="toupiao_pre.php" method="post" onsubmit="newin('')" target=homeWin>
<center>【網(wǎng)上調(diào)查】</center><br>
<?
echo " <font color=#cc3300>".$row_tp[0]."</font><br>"; //顯示主題
//顯示每個(gè)選項(xiàng)
for ($t=1;$t<$rows_tp;$t++){//從1開始是為了不選主題,原因嘛。。想想表的結(jié)構(gòu)吧
$seek=mysql_data_seek($result_tp,$t);
$list_tp=mysql_fetch_row($result_tp);
?>
<input type="radio" name="piaosu" value="<? echo $list_tp[0]."\"";if ($t==1) echo "checked"; ?> ><? echo $list_tp[0]; ?><br>
<?
}
?>
<div align=center><br><input type="submit" name="Submittp" value="投票/查看" style="background-color: rgb(235,235,235)"></div>
</form>
<?
}
?>
三、管理頁面
<?
include("connect.inc.php3");
//功能:1、更新投票主題(同時(shí)將上期投票結(jié)果放到歷史中) 2、查看歷史結(jié)果 3、修改歷史結(jié)果
?>
<form name="form1" action="<? echo $PHP_SELF; ?>" method="get" >
<font color=#ff9900><center>更新主題:</font><select name="select">
<option selected>選項(xiàng)數(shù)目</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="submit" name="Submit_tp" value="確定">
<?
echo " <a href=\"$PHP_SELF?vhistory=yes\"><font color=#ff9900>查看歷史記錄</font><a> ";
echo "<a href=\"$PHP_SELF?stop=yes\"><font color=#ff9900>停止使用投票系統(tǒng)</font></a> ";
echo "<a href=\"admin.php3\"><font color=#ff9900>文章管理</font></a>";
echo "</center></form> <hr size=\"1\" color=\"#ff9900\">";
if ($Submit_tp) {
?>
<SCRIPT language=JavaScript>
<!--
var submitcount=0;
function check_com(){
if(document.form_tp.theme.value.length ==0){
submitcount--;
alert("主題沒有填寫!\nYou must supply a subject.");
return false;
}
<?
for ($t=1;$t<=$select;$t++) {
?>
if(document.form_tp.<? echo $xuanxiang[$t]; ?>.value.length ==0){
submitcount--;
alert("選項(xiàng)沒有填寫完整\nYou must supply a option.");
return false;
}
<?
}
?>
}
//-->
</SCRIPT>
<form name="form_tp" onsubmit="return check_com()" action="<? echo $PHP_SELF; ?>" method="post" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="23%" bgcolor="#f5f5f5">
<div align="center">主 題:</div>
</td>
<td width="77%" bgcolor="#f5f5f5">
<input type="text" name="theme"><input type="hidden" name="select2" value="<? echo $select; ?>">
</td>
</tr>
<tr>
<td width="23%">
<div align="center">選 項(xiàng):</div>
</td>
<td width="77%">
<?
for ($sm=1;$sm<=$select;$sm++) {
echo "$sm<input type=\"text\" name=\"".$xuanxiang[$sm]."\" size=\"10\"> ";
}?>
</td>
</tr>
<tr>
<td colspan="2" bgcolor="#f5f5f5">
<div align="center">
<input type="submit" onSubmit="return check_com()" name="Submit_new" value="確定">
</div>
</td>
</tr>
</table>
</form>
<?
}
if ($Submit_new)
{//1
//從CURRENT表中讀出數(shù)據(jù)放到歷史結(jié)果表中
$query="select * from $current";
$result=mysql_query($query);
$row_num=mysql_num_rows($result);
$rows=mysql_fetch_array($result);
if ($rows!=0){//2
for ($i=1;$i<$row_num;$i++)
{//3
$seek=mysql_data_seek($result,$i);
$row=mysql_fetch_row($result);
$jieguo=$jieguo.$row[0]."&".$row[1]."\n";
}//4
$date=date("Y-m-d");
$query_inse="insert into $alldata (theme,idea,begin,end) values('$rows[0]','$jieguo','$rows[2]','$date')";
$insert=mysql_query($query_inse);
//5
//刪除current中原有數(shù)據(jù)
if ($insert) mysql_query("delete from $current");
}
//加入新數(shù)據(jù)
$begin=date("Y-m-d");
$query_new_theme="insert into $current (xx,date) values('$theme','$begin')";
$new_theme=mysql_query($query_new_theme);
if ($new_theme) echo "主題更改成功!<br>";
for ($n=1;$n<=$select2;$n++) {//8
$query_new_xx="insert into $current (xx) values(\"${$xuanxiang[$n]}\")";
$new_xx=mysql_query($query_new_xx);
if ($new_xx)
echo "選項(xiàng)<font color=red>${$xuanxiang[$n]}</font>添加成功!<br>";else echo "選項(xiàng)<font color=red>${$xuanxiang[$n]}</font>添加失敗!<br>";
} //9
echo "<center>3秒后返回</center><meta http-equiv=\"refresh\" content=\"3;url=$PHP_SELF\">";
} //10
//顯示歷史結(jié)果
if (isset($vhistory) && $vhistory=="yes") {
$query="select * from $alldata order by id desc";
$result=mysql_query($query);
while($rows=mysql_fetch_array($result)) {
$idea=explode("\n",$rows[idea]);
$all=0;
for ($j=0;$j<count($idea)-1;$j++) {
$allidea=explode("&",$idea[$j]);
$all=$all+$allidea[1];
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr bgcolor="#EAEAEA">
<td height="20"><? echo "【主題】 ".$rows[theme]." 【開始時(shí)間】 ".$rows[begin]." 【結(jié)束時(shí)間】 ".$rows[end]." [共".$all."人參加投票] <a href=\"$PHP_SELF?del=yes&id=$rows[id]\"><font color=red>[刪除]</font></a>"; ?></td>
</tr>
<tr>
<td>
<?
for ($j=0;$j<count($idea)-1;$j++) {
$list_idea=explode("&",$idea[$j]);
if ($all!=0) $percent=round($list_idea[1]/$all*10000)/100; else $percert=0;
echo "<font color=#cc3300>".$list_idea[0].":</font>".$list_idea[1]."人 (".$percent."%) ";
}
?> </td>
</tr>
</table>
<br><br>
<?
}
}
if (isset($del) && $del=="yes") {
$query_del="delete from $alldata where id=$id";
$result_del=mysql_query($query_del);
if ($result_del) {
echo "<center>刪除成功! 1秒后返回!</center>";
echo "<meta http-equiv=\"refresh\" content=\"1;url=$PHP_SELF?vhistory=yes\">";
}
}
//停止投票
if (isset($stop) && $stop=="yes"){
//current to alldata
$query="select * from $current";
$result=mysql_query($query);
$row_num=mysql_num_rows($result);
$rows=mysql_fetch_array($result);
if ($rows!=0){
for ($i=1;$i<$row_num;$i++)
{
$seek=mysql_data_seek($result,$i);
$row=mysql_fetch_row($result);
$jieguo=$jieguo.$row[0]."&".$row[1]."\n";
}
$date=date("Y-m-d");
$query_inse="insert into $alldata (theme,idea,begin,end) values('$rows[0]','$jieguo','$rows[2]','$date')";
$insert=mysql_query($query_inse);
}
//del
$query_stop="delete from $current";
$result_stop=mysql_query($query_stop);
if ($result_stop) echo "<center>網(wǎng)上調(diào)查已停止!</center>";else echo "<center>停止網(wǎng)上調(diào)查失!</center>";
}
?>
四、處理頁面(計(jì)數(shù)頁面)
<?
//顯示頁面
include("connect.inc.php3");
$query="update $current set ps=ps+1 where binary xx like \"$piaosu\""; //*選中的票數(shù)加1 ,binary是為了精確匹配
$update=mysql_query($query);
$query_tp="select * from $current";
$result_tp=mysql_query($query_tp);
$result_all=mysql_fetch_array($result_tp);
$result_tp_num=mysql_num_rows($result_tp);
echo "<title>$result_all[0]</title>";
for ($i=1;$i<$result_tp_num;$i++) {
$seek=mysql_data_seek($result_tp,$i);
$row=mysql_fetch_row($result_tp);
$ps[$i]=$row[1];//*把票數(shù)放到數(shù)組ZHUTI中
$zhuti[$i]=$row[0];//*把選項(xiàng)放到數(shù)組ZHUTI中
$piaosuall=$piaosuall+$ps[$i];//*總票數(shù)
}
echo "<font color=#cc3300>".$result_all[0]."</font>(共".$piaosuall."人參加投票)<br><br>"; //*顯示主題
for ($k=1;$k<$result_tp_num;$k++) {
$percent=round($ps[$k]/$piaosuall*10000)/100;//*計(jì)算每個(gè)選項(xiàng)所占的百分比
echo $zhuti[$k].":".$ps[$k]."人 (".$percent."%)<br>";//*顯示每個(gè)選項(xiàng)
}
?>
<p align="center">
<a href=
javascript:window.close()><u>關(guān) 閉</u></a></p>