一个简单的投票程序

需要文件:

index.php => 程序主体
setup.kaka => 初始化建数据库用
toupiao.php => 显示&投票

// ----------------------------- index.php ------------------------------ //

?

#咔咔投票系统正式用户版1.0
#由 游咔咔 开发设计::[email protected]
#-------------------------
#日期:2003年3月26日
#欢迎个人用户使用和扩展本系统。
#关于商业使用权,请和作者联系。
#没有作者允许,不允许将其用于商业用途,否则将追究其法律责任
##################################
############必要的数值,根据需要自己更改
// $url="localhost";//数据库服务器地址
$name="root";//数据库用户名
$pwd="";//数据库密码
//登陆用户名和密码在 login 函数里,自己改吧
$db="pol";//数据库名
##################################
#生成步骤:
#1.创建数据库
#方法:用mysqladmin直接创建,或者在mysql的command环境下,输入:create database name; 注意:下面要填上 $db="name";
#2.创建两个表语句:
#在 create table poll(pollid int(10) AUTO_INCREMENT primary key,question varchar(255) default NULL,begindate int(10) default 0,options text default NULL,votes text default NULL,deaddate int(10) default NULL,number smallint(6) default 0,oddmul smallint(1) default 0);

#create table pollvote(pollvoteid int(10) AUTO_INCREMENT primary key,pollid int(10) default 0,votequestion varchar(255) default NULL,votenumber varchar(255) default NULL,userip varchar(15) default NULL,votedate int(10) default NULL);

#如果需要更详尽的功能 和 版本 请和我联系:[email protected]
########################################################################

############函数模块
function login( $user, $password)#验证用户名和密码功能
{
if( $user=="ukaka"&& $password=="123")#在这里设置用户名和密码
{return(TRUE);}
else
{return(FALSE);}
}
function sql_connect( $url, $name, $pwd)#与数据库进行连接
{
if(!strlen( $url))
{ $url="localhost";}
if(!strlen( $name))
{ $name="root";}
if(!strlen( $pwd))
{ $pwd="";}
return mysql_connect( $url, $name, $pwd);
}
##################

if( $fp=@fopen("setup.kaka","r")) //建立初始化数据库
{
require("./setup.kaka");
$myconn=sql_connect( $url, $name, $pwd);
@mysql_create_db( $db, $myconn);
mysql_select_db( $db, $myconn);
$strPollD="drop table poll";
$strPollvoteD="drop table pollvote";
$result=@mysql_query( $strPollD, $myconn);
$result=@mysql_query( $strPollvoteD, $myconn);
$result=mysql_query( $strPoll, $myconn) or die(mysql_error());
$result=mysql_query( $strPollvote, $myconn) or die(mysql_error());
mysql_close( $myconn);
fclose( $fp);
@unlink("setup.kaka");
}
?>

  1<html>
  2<head>
  3<meta content="zh-cn" http-equiv="Content-Language"/>
  4<meta content="Microsoft FrontPage 5.0" name="GENERATOR"/>
  5<style type="text/css">   
  6<!--   
  7input { font-size:9pt;}   
  8A:link {text-decoration: underline; font-size:9pt;color:000059}   
  9A:visited {text-decoration: underline; font-size:9pt;color:000059}   
 10A:active {text-decoration: none; font-size:9pt}   
 11A:hover {text-decoration:underline;color:red}   
 12body, table {font-size: 9pt}   
 13tr, td{font-size:9pt}   
 14\-->   
 15</style>
 16<title>咔咔投票系统###by  [email protected]  </title>
 17</head>
 18<body alink="#FF0000" bgcolor="#E5E5E5" leftmargin="0" link="#000099" topmargin="8" vlink="#CC6600">
 19<div align="center">
 20<center>
 21<table border="0" bordercolor="#111111" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="550">
 22<tr>
 23<td width="100%"> </td>
 24</tr>
 25<tr>
 26<td align="center" width="100%">
 27<table border="1" bordercolor="#111111" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
 28<tr>
 29<td align="center" background="bg1.gif" width="100%">
 30<a href="mailto:[email protected]"><img alt="与我联系" border="0" height="30" src="log1.gif" width="300"/></a></td>
 31</tr>
 32<tr>
 33<td align="center" bgcolor="#E5E5E5" width="100%">
 34<?   
 35if(!login( $user, $password)) #登陆验证   
 36{   
 37?>
 38<form action="" method="get">
 39<table border="0" bordercolor="#111111" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="260">
 40<tr>
 41<td width="30%"> </td><td width="70%"> </td>
 42</tr>
 43<tr>
 44<td width="30%">
 45<img border="0" height="28" src="name.gif" width="80"/></td><td width="70%">
 46<input name="user" size="20"/></td>
 47</tr>
 48<tr>
 49<td width="30%">
 50<img border="0" height="28" src="password.gif" width="80"/></td><td width="70%">
 51<input name="password" size="20" type="password"/></td>
 52</tr>
 53<tr>
 54<td width="30%"> </td><td width="70%"> </td>
 55</tr>
 56<tr>
 57<td align="center" colspan="2" width="100%"><input name="poll" type="submit" value="投票系统"/><input type="reset" value="重新填写"/><input name="admin" type="submit" value="管理系统"/></td>
 58</tr>
 59<tr>
 60<td align="center" colspan="2" width="100%"></td>
 61</tr>
 62</table></form>
 63<?   
 64}   
 65else#登陆成功,进行功能模块选择   
 66{#A   
 67if(strlen( $poll))   
 68{#B:投票系统####################################   
 69if(strlen( $modifynumber)||strlen( $question)==0||strlen( $deaddate)==0||strlen( $pol[1])==0||strlen( $pol[2])==0)   
 70{#C   
 71?> <div align="center">
 72<form action="&lt;? echo $PHP_SELF?&gt;" method="get" name="poll">
 73<input name="user" type="hidden" value="&lt;?echo $user?&gt;"/>
 74<input name="password" type="hidden" value="&lt;?echo $password?&gt;"/>
 75<input name="poll" type="hidden" value="on"/>
 76<center>
 77<table border="1" cellspacing="1" style="border-collapse: collapse" width="550">
 78<tr><td colspan="2" width="494"> 发布一个投票</td></tr>
 79<tr><td width="119"> 投票主题</td><td width="371"><input name="question" size="20" value="&lt;?echo $question?&gt;"/></td></tr>
 80<tr><td width="119"> 投票选项数</td><td width="371"><input name="number" size="20" value="&lt;?echo $number?&gt;"/>
 81<input name="modifynumber" type="submit" value="更新投票数目"/></td></tr>
 82<tr><td width="119"> 选项</td><td width="371">请注意投票项的简洁和清楚   
 83<?#################进行投票数目的循环   
 84if( $number<2)   
 85{   
 86?>
 87<font color="ff0000"><br/>你的投票数设置错误,这样的投票是毫无意思的,请重新填写选项数.</font>
 88<?   
 89}   
 90else   
 91{   
 92for( $s=1; $s<= $number; $s++)   
 93{   
 94echo "<br>第". $s."项:<input $pol[="" $s]\"="" name='\"pol[' size='\"20\"' type='\"text\"' value='\"'/>";   
 95if( $s==1|| $s==2) { echo "<font color='\"ff0000\"'> [注意:此项必须填写]</font>";}   
 96}   
 97}   
 98?&gt;   
 99</td></tr>
100<tr><td width="119"> 单选/可复选</td><td width="371"><select name="oddmul" size="1"><option selected="" value="1">单选</option><option value="0">复选</option></select></td></tr>
101<tr><td width="119"> 投票时限</td><td width="371"><input name="deaddate" size="20" value="0"/>天(无限制请填0天)</td></tr>
102<tr><td align="center" colspan="2" width="494"><input name="poll" type="submit" value="提交查询"/><input type="reset" value="重新输入"/></td></tr>
103</table></center></form>
104</div>
105<?   
106}#C   
107else#提交填写的内容进入数据库   
108{#D   
109$begindate=time();   
110$deaddate= $deaddate*86400+time();   
111$options= $pol[1];   
112$votes=0;   
113for( $j=2; $j<= $number; $j++)#复杂了,记着改进算法   
114{   
115if(strlen( $pol[ $j]))   
116{   
117$options= $options."|||". $pol[ $j];   
118$votes= $votes."|||0";   
119}   
120}   
121$myconn=sql_connect( $url, $name, $pwd);   
122mysql_select_db( $db, $myconn);   
123$strSql=" select * from poll where question=' $question'";   
124$result=mysql_query( $strSql, $myconn) or die(mysql_error());   
125$row=mysql_fetch_array( $result);   
126if( $row)   
127{ echo" <br><font color='\"ff0000\"'>警告:该投票已经存在如有疑问</font><br/><br/>请查看 <a $password&admin='on\"' $php_self?&user="$user&amp;password=" href='\"'>管理系统</a><br/><br/><a $row[pollid]\"="" href='\"toupiao.php?id='>直接进入投票界面</a> <br/> <br/>"; #这里留有扩展   
128}   
129else   
130{   
131$strSql="insert into poll(question,begindate,options,votes,deaddate,number,oddmul) values(' $question',' $begindate',' $options',' $votes',' $deaddate',' $number',' $oddmul')";   
132$result=mysql_query( $strSql, $myconn) or die(mysql_error());   
133$strSql=" select * from poll where question=' $question'";   
134$result=mysql_query( $strSql, $myconn) or die(mysql_error());   
135$row=mysql_fetch_array( $result);   
136echo "<br/>投票生成<br/><br/>已经成功添加投票内容入数据库!<br/><br/>
137<a $row[pollid]\"="" href='\"toupiao.php?id='>进入投票界面</a><br/><br/>你可以直接将投票地址添加到你的页面环境中<br/><br/>需要特制投票页面请 <a href='\"mailto:[email protected]\"'>和我联系</a><br/><br/>欢迎访问 酷易资讯 <a href='\"http://www.coole8.com\"'> http://www.coole8.com  </a><br/><br/><font color='\"ff0000\"'>我们为您提供自制的各种优秀系统和程序</font><br/><br/>";   
138mysql_close( $myconn);   
139}   
140  
141  
142  
143}#D   
144}#B   
145if(strlen( $admin))   
146{#C:管理系统####################################   
147  
148  
149$myconn=sql_connect( $url, $name, $pwd);   
150mysql_select_db( $db, $myconn);   
151  
152if(strlen( $delnote))#处理删除单个访问者命令   
153{   
154$strSql="delete from pollvote where pollvoteid=' $delnote'";   
155mysql_query( $strSql, $myconn);   
156}   
157if(strlen( $delete))#处理删除投票的命令   
158{   
159$strSql="delete from poll where pollid=' $id'";   
160mysql_query( $strSql, $myconn);   
161}   
162if(strlen( $note))#处理投票记录的命令   
163{ $strSql="select * from pollvote where pollid=' $id' order by votedate desc";   
164$result=mysql_query( $strSql, $myconn);   
165$row=mysql_fetch_array( $result);   
166echo "<table border='\"1\"' bordercolor='\"#111111\"' cellspacing='\"1\"' collapse\"="" style='\"border-collapse:' width='\"550\"'><tr><td colspan="5">投票题目:<font color='\"ff0000\"'> $row[votequestion]</font> 注:按投票时间降序排列</td></tr>";   
167$x=1;   
168while( $row)   
169{   
170$time=date("于Y年n月d日H时I分投票", $row[votedate]);   
171echo "<tr><td> $x</td><td> 选择的结果: $row[votenumber]</td><td>来自IP: $row[userip]</td><td> $time</td><td><a $phpself."?id="$row[pollid]&amp;user=" $row[pollvoteid]\"="" $user&password="$password&amp;admin=1¬e=on&amp;delnote=" href='\"".'>删除这条记录</a></td></tr>";   
172$row=mysql_fetch_array( $result); $x++;   
173}   
174echo "</table><br/>";   
175}   
176  
177$strSql="select * from poll";   
178$result=mysql_query( $strSql, $myconn);   
179$i=mysql_num_rows( $result);   
180$color=1; $z=1;   
181echo "<div align='\"left\"'>目前有". $i."个投票主题<table bordercolor='\"#111111\"' cellspacing='\"1\"' collapse\"="" style='\"border-collapse:' width='\"550\"'>";   
182while( $rows=mysql_fetch_array( $result))   
183{   
184if( $color==1)   
185{ $colo="#e2e2e2"; $color++;}   
186else   
187{ $colo="#e9e9e9"; $color--;}   
188echo "<tr><td $colo\"="" align='\"center\"' bgcolor='\"' width='\"5%\"'> $z</td><td $colo\"="" bgcolor='\"' width='\"55%\"'> $rows[question]</td><td $colo\"="" bgcolor='\"' width='\"10%\"'><a $phpself."?id="$rows[pollid]&amp;user=" $user&password='$password&amp;admin=1&amp;delete=on\"' href='\"".'>删除投票</a></td><td $colo\"="" bgcolor='\"' width='\"10%\"'><a $phpself."?id="$rows[pollid]&amp;user=" $user&password='$password&amp;admin=1¬e=on\"' href='\"".'>投票记录</a></td><td $colo\"="" bgcolor='\"' width='\"10%\"'>
189<a $rows[pollid]&toupiao='-1\"' href='\"toupiao.php?id=' target='\"_blank\"'>查看结果</a></td></tr>"; $z++;   
190}   
191  
192echo "<tr><td align='\"right\"' colspan="4"></td></tr></table>";   
193mysql_close();   
194  
195}#C#############################################   
196}#A   
197?&gt;   
198</div></td>
199</tr>
200<tr>
201<td align="center" background="bg2.gif" colspan="2" height="30" width="100%">
202<a href="http://www.coole8.com"><img border="0" height="30" src="log2.gif" width="300"/></a></td>
203</tr>
204</table>
205</td>
206</tr>
207<tr>
208<td width="100%"> </td>
209</tr>
210</table>
211</center>
212</div>
213</body>
214</html>

// ----------------------------------------- setup.kaka -------------------------------------- //

// ---------------------------------------- toupiao.php -------------------------------------- //

  1<html>
  2<head>
  3<meta content="zh-cn" http-equiv="Content-Language"/>
  4<meta content="Microsoft FrontPage 5.0" name="GENERATOR"/>
  5<style type="text/css">   
  6<!--   
  7P {CURSOR: default; FONT-FAMILY: MS Shell Dlg,Tahoma, sans-serif; FONT-SIZE: 9pt}   
  8input { font-size:9pt;}   
  9A:link {text-decoration: underline; font-size:9pt;color:000059}   
 10A:visited {text-decoration: underline; font-size:9pt;color:000059}   
 11A:active {text-decoration: none; font-size:9pt}   
 12A:hover {text-decoration:underline;color:red}   
 13body, table {font-size: 9pt}   
 14tr, td{font-size:9pt}   
 15\-->   
 16</style>
 17<title>poll ####by  [email protected]  </title>
 18</head>
 19<body bgcolor="#EFEFEF">
 20<div align="center">
 21<?   
 22if(strlen( $id)&&strlen( $toupiao)==0)   
 23{   
 24$myconn=sql_connect( $url, $user, $pwd);   
 25mysql_select_db( $db, $myconn);   
 26$strSql="select * from poll where pollid=' $id'";   
 27$result=mysql_query( $strSql, $myconn) or die(mysql_error());   
 28$row=mysql_fetch_array( $result);   
 29?>
 30<form action="&lt;?echo $php_self?&gt;" method="get"><table border="1" bordercolor="#111111" cellspacing="1" style="border-collapse: collapse" width="20%">
 31<tr height="25"><td>★在线调查</td></tr>
 32<tr height="25"><td><?echo $row[question]?> </td></tr>
 33<tr><td><input name="id" type="hidden" value="&lt;?echo $id?&gt;"/>
 34<?   
 35$options=explode("|||", $row[options]);   
 36$y=0;   
 37while( $options[ $y])   
 38{   
 39#####################   
 40if( $row[oddmul])   
 41{   
 42echo "<input name=toupiao type=radio value= $y> $options[ $y]<br/>";   
 43}   
 44else   
 45{   
 46echo "<input $y]="" name="toupiao[" type="checkbox" value="1"/> $options[ $y]<br/>";   
 47}   
 48$y++;   
 49  
 50}   
 51?&gt;   
 52  
 53</td></tr>
 54<tr align="center" height="30"><td><input name="y" type="hidden" value="&lt;?echo $y?&gt;"/><input type="submit" value="提交查看结果"/><input type="reset" value="重新选择"/>
 55</td></tr></table></form>
 56<?   
 57mysql_close( $myconn);   
 58}   
 59else   
 60{   
 61$myconn=sql_connect( $url, $user, $pwd);   
 62mysql_select_db( $db, $myconn);   
 63$strSql="select * from poll where pollid=' $id'";   
 64$result=mysql_query( $strSql, $myconn) or die(mysql_error());   
 65$row=mysql_fetch_array( $result);   
 66$votequestion= $row[question];   
 67$oddmul= $row[oddmul];   
 68$time=time();   
 69if( $row[deadtime]< $time&& $row[deadtime]!= $row[begintime])   
 70{   
 71$timeread="<br><font color='\"ff0000\"'>已经超过投票期限!你无权再投票</font>";   
 72}   
 73else   
 74{   
 75########################################   
 76// $votes=explode("|||", $row[votes]);   
 77// $options=explode("|||", $row[options]);   
 78  
 79if( $oddmul)##单个选区域   
 80{   
 81$m=ifvote( $id, $REMOTE_ADDR);   
 82if(! $m)   
 83{vote( $toupiao, $id, $REMOTE_ADDR);}   
 84}   
 85else##可复选区域 #############这里有需要改进的地方   
 86{   
 87$x=0;   
 88while(list( $k, $v)=each( $toupiao))   
 89{   
 90if( $v==1)   
 91{ vote( $k, $id, $REMOTE_ADDR);}   
 92}   
 93}   
 94}   
 95  
 96  
 97?&gt;   
 98<table border="1" bordercolor="#111111" cellspacing="1" style="border-collapse: collapse">
 99<tr height="25"><td colspan="2">在线调查结果</td></tr>
100<tr height="25"><td colspan="2"><?echo $row[question]. $timeread?> </td></tr>
101<?   
102$strSql="select * from poll where pollid=' $id'";   
103$result=mysql_query( $strSql, $myconn) or die(mysql_error());   
104$row=mysql_fetch_array( $result);   
105$options=explode("|||", $row[options]);   
106$votes=explode("|||", $row[votes]);   
107$x=0;   
108while( $options[ $x])   
109{   
110$total+= $votes[ $x];   
111$x++;   
112}   
113$x=0;   
114while( $options[ $x])   
115{   
116$r= $x%5;   
117$tot=0;   
118if( $total!=0)   
119{   
120$tot= $votes[ $x]*100/ $total;   
121$tot=round( $tot,2);   
122}   
123echo "<tr><td> $options[ $x]</td><td><image height='\"10\"' src='\"l.gif\"' width='\"1\"'/><image $r.gif\"="" $votes[="" $x]\"="" height='\"10\"' src='\"' width='\"'/><image height='\"10\"' src='\"r.gif\"' width='\"1\"'/> 共 $votes[ $x]票,占 $tot%</td>";   
124$x++;   
125}   
126echo "<tr height='\"25\"'><td colspan="2">总计:". $total."票</td></tr>";   
127if(strlen( $m))   
128{echo "<tr height='\"25\"'><td colspan="2"><font color='\"ff0000\"'> $m</font></td></tr>";}   
129?&gt;   
130</table>
131<? mysql_close( $myconn);   
132}   
133?>
134<hr size="1" width="200"/>
135<a href="http://www.coole8.com">coole8</a> 版权所有   
136</div>
137</body>
138</html>

// end

到这里一个投票程序就写好了~~还有什么问题就来这里问吧

Published At
Categories with Web编程
Tagged with
comments powered by Disqus