作者:转接卡 建立2个表,一个用来显示当前主题,一个存放历史结果。 当前主题表结构:选项(包括主题),票数 历史结果表结构:id,主题名,关点,投票开始时间,投票结束时间 管理页面: 功能:1、更新投票主题 2、查看历史结果 3、停止使用投票系统 一、建表 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中,第一个记录存放主题和建立时间,在进行投票时不改动。从第二个记录开始记录各选项内容和票数。 链接程序: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>【网上调查】</center><br> <? echo " <font color=#cc3300>".$row_tp[0]."</font><br>"; //显示主题 //显示每个选项 for ($t=1;$t<$rows_tp;$t++){//从1开始是为了不选主题,原因嘛。。想想表的结构吧 $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、更新投票主题(同时将上期投票结果放到历史中) 2、查看历史结果 3、修改历史结果 ?> <form name="form1" action="<? echo $PHP_SELF; ?>" method="get" > <font color=#ff9900><center>更新主题:</font><select name="select"> <option selected>选项数目</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>停止使用投票系统</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("选项没有填写完整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">选 项:</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表中读出数据放到历史结果表中 $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中原有数据 if ($insert) mysql_query("delete from $current"); } //加入新数据 $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 "选项<font color=red>${$xuanxiang[$n]}</font>添加成功!<br>";else echo "选项<font color=red>${$xuanxiang[$n]}</font>添加失败!<br>"; } //9 echo "<center>3秒后返回</center><meta http-equiv="refresh" content="3;url=$PHP_SELF">"; } //10 //显示历史结果 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]." 【开始时间】 ".$rows[begin]." 【结束时间】 ".$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>网上调查已停止!</center>";else echo "<center>停止网上调查失败!</center>"; } ?> 四、处理页面(计数页面) <? //显示页面 include("connect.inc.php3"); $query="update $current set ps=ps+1 where binary xx like "$piaosu""; //*选中的票数加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];//*把票数放到数组ZHUTI中 $zhuti[$i]=$row[0];//*把选项放到数组ZHUTI中 $piaosuall=$piaosuall+$ps[$i];//*总票数 } 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;//*计算每个选项所占的百分比 echo $zhuti[$k].":".$ps[$k]."人 (".$percent."%)<br>";//*显示每个选项 } ?> <p align="center"> <a href=javascript:window.close()><u>关 闭</u></a></p> |