您好,欢迎访问一九零五行业门户网

PHP+MySQL投票系统的设计和实现分享_PHP教程

系统不大,完成这个系统的过程我分了三个步骤
•数据库设计
•系统框架设计
•前端美化
数据库的设计
设计三张表:投票结果统计表(count_voting),投票人记录表(ip_votes),用户表(user)
投票结果统计表用于统计最后的投票记录,我给它弄了4个字段:被投票项的名称(selectname),被投票项标签名(labelname)(起到分类的作用),票数(countvotes)。
投票人记录表用于登记投票人的ip(ip),地理位置(location),投票时间(votetime),被投票项名称(selectname)。然后我还给它加一个id。
用户表主要用于给管理员用的,包含用户名(name)和密码(passwd)。
生成表的sql脚本如下:
复制代码 代码如下:
--
-- 表的结构 `count_voting`
--
drop table if exists `count_voting`;
create table if not exists `count_voting` (
`selectname` varchar(40) not null,
`labelname` varchar(40) not null,
`countvotes` bigint(20) unsigned not null,
unique key `selectname` (`selectname`),
key `countvotes` (`countvotes`),
key `countvotes_2` (`countvotes`),
key `countvotes_3` (`countvotes`)
) engine=innodb default charset=utf8 comment='投票统计表';
-- --------------------------------------------------------
--
-- 表的结构 `ip_votes`
--
drop table if exists `ip_votes`;
create table if not exists `ip_votes` (
`id` bigint(20) unsigned not null auto_increment comment '投票人序号:自增',
`ip` varchar(15) not null comment '投票人ip',
`location` varchar(40) not null comment '投票人位置',
`votetime` datetime not null,
`selectname` varchar(40) not null,
primary key (`id`),
key `id` (`id`),
key `selectname` (`selectname`)
) engine=innodb default charset=utf8 auto_increment=4 ;
--
-- 触发器 `ip_votes`
--
drop trigger if exists `vote_count_after_insert_tr`;
delimiter //
create trigger `vote_count_after_insert_tr` after insert on `ip_votes`
for each row update count_voting set countvotes = countvotes + 1 where selectname = new.selectname
//
delimiter ;
-- --------------------------------------------------------
--
-- 表的结构 `user`
--
drop table if exists `user`;
create table if not exists `user` (
`name` varchar(10) not null comment '管理员用户名',
`passwd` char(32) not null comment '登录密码md5值'
) engine=innodb default charset=utf8 comment='用户表';
--
-- 转存表中的数据 `user`
--
insert into `user` (`name`, `passwd`) values
('ttxi', '700469ca1555900b18c641bf7b0a1fa1'),
('jitttanwa', 'adac5659956d68bcbc6f40aa5cd00d5c');
--
-- 限制导出的表
--
--
-- 限制表 `ip_votes`
--
alter table `ip_votes`
add constraint `ip_votes_ibfk_1` foreign key (`selectname`) references `count_voting` (`selectname`) on delete cascade on update cascade;
从脚本中可以看出,我创建了一个触发器,当往ip_votes表中插入数据的时候就给count_voting表中的countvotes字段加1。还能后出最后一句是设置外部关联字。
框架设计
operatordb类用于操作数据库,operatorvotingdb类用于该系统特定的操作集合。
使用pdo操作数据库,我它简单的封装一下:
复制代码 代码如下:
/**
* 操作数据库
* 封装pdo,使其方便自己的操作
*/
class operatordb
{
//连接数据库的基本信息
private $dbms='mysql'; //数据库类型,对于开发者来说,使用不同的数据库,只要改这个.
private $host='localhost'; //数据库主机名
private $dbname='voting'; //使用的数据库
private $user='voting'; //数据库连接用户名
private $passwd='voting'; //对应的密码
private $pdo=null;
public function __construct()
{
//dl(php_pdo.dll);
//dl(php_pdo_mysql.dll);
$this->dsn=$this->dbms:host=$this->host;dbname=$this->dbname;
try
{
$this->conn=new pdo($this->dsn,$this->user,$this->passwd);//初始化一个pdo对象,就是创建了数据库连接对象$db
}
catch(pdoexception $e)
{
die(
数据库连接失败(creater pdo error!): .$e->getmessage().
);
}
}
public function __destruct()
{
$this->pdo = null;
}
public function exec($sql)
{
}
public function query($sql)
{
}
}
把连接数据库的信息封装进去方便后续的操作。
复制代码 代码如下:
odb = new operatordb();
}
public function __destruct()
{
$this->odb = null;
}
/**
* 清空voting数据中的所有表
*
* 调用数据库操作类,执行clear数据库的操作
*/
public function cleartables()
{
$sqls = array(truncate ip_votes;,truncate count_voting;);
$this->odb->exec($sqls[0]);
$this->odb->exec($sqls[1]);
}
/**
* 重置count_voting表中的countvalues字段为0
*
*/
public function resetcountvalues()
{
$sql = update count_voting set countvotes = 0;;
$this->odb->exec($sql);
}
/**
* 投票
* 将信息写入ip_votes表
* @param type $ip
* @param type $loc
* @param type $time
* @param type $name
*/
public function vote($ip,$loc,$name)
{
$sql = insert into ip_votes values (null, '$ip', '$loc', now(), '$name');
$subsql = select max(to_days(votetime)) from ip_votes where ip='$ip';
$stm = $this->odb->query($subsql);
if (count($row=$stm->fetchall())==1)
{
$now = date(y-m-d h:i:s);
$subsql = select to_days('$now');;
$stm = $this->odb->query($subsql)->fetch();
$time = $stm[0];//使用mysql计算出的today时间
// echo $time.
;
// echo $row[0][0];
if ($time-$row[0][0]{
echo 投票失败,相同ip需要隔一天才能投票;
return;
}
}
// echo $sql;
echo 投票成功!;
$this->odb->exec($sql);
}
/**
* 添加selectname字段的行
*
* @param string $name
* @param string $label
* @param int $count
*/
public function addselectname($name, $label, $count=0)
{
$sql = insert into count_voting values ('$name', '$label', $count);;
$this->odb->exec($sql);
}
/**
* 获取总投票情况,按票数排序的结果
*
* 按countvotes字段排序,返回count_voting表
*
* @param int $n
*
*/
public function getvotessortbycount($n=-1)
{
$sql = select * from count_voting order by countvotes desc limit 0 , $n;;
if (-1 == $n)
{
$sql = select * from count_voting order by countvotes desc;;
}
// echo $sql;
return $this->odb->query($sql);
}
/**
* 获取投票情况,按票数排序并按标签分组的结果
*
* 按countvotes字段排序并按labelname字段分组,返回count_voting表
*/
public function getvotesgroupbylabel()
{
$sql = select * from count_voting order by labelname desc;;
// echo $sql;
return $this->odb->query($sql);
}
}
?>
下面还有需要的函数
复制代码 代码如下:
然后就是后台管理员的操作怎么弄了,主要是添加投票项的功能,操作数据库上面已经实现。后面的基本上是页面怎么设置,关系到js。添加投票项的页面是动态的,如下:
复制代码 代码如下:
function addvote()
{
right.innerhtml=添加投票项;
right.innerhtml+=投票项标签;
addinput(right,clabelname,地区名);
right.innerhtml+=
投票项名称;
addinput(right,cselectname,学校名);
right.innerhtml+=
;
var args = '\'./add.php\',\'cselectname\',\'clabelname\'';
var str = '';
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);
}
效果:
清空投票项也差不多,下过如下:
添加投票项是通过url传递变量到add.php页面的。
复制代码 代码如下:
addselectname($name,$label);
require './header.htm';
gotopgae(./admin.php?page=add&auto=.$label.&id=clabelname&foc=cselectname&msg=添加成功);
?>
下面是两个跳转页面的函数,js的(上面func.php中的跳转页面函数也是通过js实现的)。
复制代码 代码如下:
//js
function gotopage(url,arg1,arg2)
{
var a = document.getelementbyid(arg1).value;
var b = document.getelementbyid(arg2).value;
url += '?'+arg1+'='+a;
url += '&'+arg2+'='+b;
window.location.href=url;
}
function gotopage1(url)
{
window.location.href=url;
}
还有修改删除功能没有实现。我应该不会去实现那个了吧,js的话和添加功能差不多。
登录模块的话网上很多,模仿的。就是提交表单,查找数据库,返回结果。成功则设置cookie,后台的每个页面都添加了检测cookie的功能的。
前端美化
index.php页面首先操作数据库获取投票项和票数,然后显示出来(通过css+div美化一下框架界面什么的),最后点击投票按钮就提交表单,跳转到vote.php页面。
css的话我都是抄网上的。我弄的效果如下:
这个东西算是个很小的信息管理系统吧,我已经把这个东西的源代码放到github(https://github.com/hanxi/voting)上去了,可以随意下载修改也可以到脚本之家下载(点击下载)。欢迎读者回复交流,这方面不是我的强项,有很多不足之处还望指教。
作者:涵曦(涵曦的技术博客 - 博客园)
微博:t.qq.com/hanxi1203
出处:hanxi.cnblogs.com
http://www.bkjia.com/phpjc/325961.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/325961.htmltecharticle系统不大,完成这个系统的过程我分了三个步骤 数据库设计 系统框架设计 前端美化 数据库的设计 设计三张表:投票结果统计表(count_vo...
其它类似信息

推荐信息