本文章详细的介绍了关于投票系统实现原理与实现代码,有需要的朋友可参考一下。
数据库的设计
设计三张表:投票结果统计表(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);
}
}
?>
1 2 3
http://www.bkjia.com/phpjc/444675.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/444675.htmltecharticle本文章详细的介绍了关于投票系统实现原理与实现代码,有需要的朋友可参考一下。 数据库的设计 设计三张表:投票结果统计表(count_vo...