系统不大,完成这个系统的过程我分了三个步骤
•数据库设计
•系统框架设计
•前端美化
[b]数据库的设计[/b]
设计三张表:投票结果统计表(count_voting),投票人记录表(ip_votes),用户表(user)
投票结果统计表用于统计最后的投票记录,我给它弄了4个字段:被投票项的名称(SelectName),被投票项标签名(LabelName)(起到分类的作用),票数(CountVotes)。
投票人记录表用于登记投票人的ip(IP),地理位置(Location),投票时间(VoteTime),被投票项名称(SelectName)。然后我还给它加一个ID。
用户表主要用于给管理员用的,包含用户名(name)和密码(passwd)。
生成表的sql脚本如下:
[url=$url]echo "</script>";
}
function jsFunc($fun, $arg=null)
{
echo "<script language='javascript' type='text/javascript'>";
echo $fun."('$arg');";
echo "</script>";
}
function jsFunc3($fun, $arg1=null,$arg2=null,$arg3=null)
{
echo "<script language='javascript' type='text/javascript'>";
echo $fun."('$arg1','$arg2','$arg3');";
echo "</script>";
//echo $fun."('$arg1','$arg2','$arg3');";
}
function isLoginNow()
{
if ($_COOKIE["user"]=='')
{
return false;
}
return true;
}
function getClientIP()
{
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
{
if ($_SERVER["HTTP_CLIENT_IP"])
{
$proxy = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$proxy = $_SERVER["REMOTE_ADDR"];
}
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
if ($_SERVER["HTTP_CLIENT_IP"])
{
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$ip = $_SERVER["REMOTE_ADDR"];
}
}
return $ip;
}
//从123查获取ip
function getIpfrom123cha($ip) {
$url = 'http://www.123cha.com/ip/?q='.$ip;
$content = file_get_contents($url);
$preg = '/(?<=本站主数据:<\/li><li style=\"width:450px;\">)(.*)(?=<\/li>)/isU';
preg_match_all($preg, $content, $mb);
$str = strip_tags($mb[0][0]);
//$str = str_replace(' ', '', $str);
$address = $str;
if($address == '') {
$address = '未明';
}
return $address;
}
//从百度获取ip所在地
function getIpfromBaidu($ip) {
$url = 'http://www.baidu.com/s?wd='.$ip;
$content = file_get_contents($url);
$preg = '/(?<=<p class=\"op_ip_detail\">)(.*)(?=<\/p>)/isU';
preg_match_all($preg, $content, $mb);
$str = strip_tags($mb[0][1]);
$str = str_replace(' ', '', $str);
$address = substr($str, 7);
if($address == '') {
$address = '未明';
}
return $address;
}
?>
然后就是后台管理员的操作怎么弄了,主要是添加投票项的功能,操作数据库上面已经实现。后面的基本上是页面怎么设置,关系到js。添加投票项的页面是动态的,如下:
function addVote()
{
right.innerHTML="<h2>添加投票项</h2>";
right.innerHTML+="<label>投票项标签<label>";
addInput("right","cLabelName","地区名");
right.innerHTML+="<br><label>投票项名称<label>";
addInput("right","cSelectName","学校名");
right.innerHTML+="<br>";
var args = '\'./add.php\',\'cSelectName\',\'cLabelName\'';
var str = '<input type=button value="\u6dfb加" onclick="goToPage('+args+');"/>';
right.innerHTML+=str;
}
//添加文本框
function addInput(parent,id,pla)
{
//创建input
var input = document.createElement("input");
input.type = "text";
input.id = id;
input.placeholder = pla;
document.getElementById(parent).appendChild(input);
}
效果:
[img]http://files.jb51.net/file_images/article/201209/201209231943402.png[/img]
清空投票项也差不多,下过如下:
[img]http://files.jb51.net/file_images/article/201209/201209231943403.png[/img]
添加投票项是通过url传递变量到add.php页面的。
[url=https://github.com/hanxi/voting]https://github.com/hanxi/voting[/url])上去了,可以随意下载修改也可以到编程素材网下载([url=http://xiazai.jb51.net/201209/yuanma/hanxi_voting_php.rar]点击下载[/url])。欢迎读者回复交流,这方面不是我的强项,有很多不足之处还望指教。
作者:涵曦(涵曦的技术博客 - 博客园)
微博:t.qq.com/hanxi1203
出处:hanxi.cnblogs.com