系統(tǒng)不大,完成這個(gè)系統(tǒng)的過程我分了三個(gè)步驟 - 數(shù)據(jù)庫設(shè)計(jì)
- 系統(tǒng)框架設(shè)計(jì)
- 前端美化
數(shù)據(jù)庫的設(shè)計(jì)設(shè)計(jì)三張表:投票結(jié)果統(tǒng)計(jì)表(count_voting),投票人記錄表(ip_votes),用戶表(user) 投票結(jié)果統(tǒng)計(jì)表用于統(tǒng)計(jì)最后的投票記錄,我給它弄了4個(gè)字段:被投票項(xiàng)的名稱(SelectName),被投票項(xiàng)標(biāo)簽名(LabelName)(起到分類的作用),票數(shù)(CountVotes)。 投票人記錄表用于登記投票人的ip(IP),地理位置(Location),投票時(shí)間(VoteTime),被投票項(xiàng)名稱(SelectName)。然后我還給它加一個(gè)ID。 用戶表主要用于給管理員用的,包含用戶名(name)和密碼(passwd)。 生成表的sql腳本如下: -- -- 表的結(jié)構(gòu) `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='投票統(tǒng)計(jì)表';
-- --------------------------------------------------------
-- -- 表的結(jié)構(gòu) `ip_votes` --
DROP TABLE IF EXISTS `ip_votes`; CREATE TABLE IF NOT EXISTS `ip_votes` ( `ID` bigint(20) unsigned NOT NULL auto_increment COMMENT '投票人序號(hào):自增', `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 ;
-- -- 觸發(fā)器 `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 ;
-- --------------------------------------------------------
-- -- 表的結(jié)構(gòu) `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='用戶表';
-- -- 轉(zhuǎn)存表中的數(shù)據(jù) `user` --
INSERT INTO `user` (`name`, `passwd`) VALUES ('ttxi', '700469ca1555900b18c641bf7b0a1fa1'), ('jitttanwa', 'adac5659956d68bcbc6f40aa5cd00d5c');
-- -- 限制導(dǎo)出的表 --
-- -- 限制表 `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;
框架設(shè)計(jì)OperatorDB類用于操作數(shù)據(jù)庫,OperatorVotingDB類用于該系統(tǒng)特定的操作集合。 使用PDO操作數(shù)據(jù)庫,我它簡(jiǎn)單的封裝一下:
/** * 操作數(shù)據(jù)庫 * 封裝PDO,使其方便自己的操作 */ class OperatorDB { //連接數(shù)據(jù)庫的基本信息 private $dbms='mysql'; //數(shù)據(jù)庫類型,對(duì)于開發(fā)者來說,使用不同的數(shù)據(jù)庫,只要改這個(gè). private $host='localhost'; //數(shù)據(jù)庫主機(jī)名 private $dbName='voting'; //使用的數(shù)據(jù)庫 private $user='voting'; //數(shù)據(jù)庫連接用戶名 private $passwd='voting'; //對(duì)應(yīng)的密碼 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);//初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫連接對(duì)象$db } catch(PDOException $e) { die("<br/>數(shù)據(jù)庫連接失敗(creater PDO Error!): ".$e->getMessage()."<br/>"); } } public function __destruct() { $this->pdo = null; } public function exec($sql) { } public function query($sql) { } }
<?php require_once 'OperatorDB.php'; class OperatorVotingDB { private $odb;
public function __construct() { $this->odb = new OperatorDB(); } public function __destruct() { $this->odb = null; }
/** * 清空Voting數(shù)據(jù)中的所有表 * * 調(diào)用數(shù)據(jù)庫操作類,執(zhí)行clear數(shù)據(jù)庫的操作 */ 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計(jì)算出的today時(shí)間 // echo $time."<br>"; // echo $row[0][0]; if ($time-$row[0][0]<1)//表中最大的時(shí)間和現(xiàn)在的時(shí)間$time比較 { 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); }
/** * 獲取總投票情況,按票數(shù)排序的結(jié)果 * * 按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); }
/** * 獲取投票情況,按票數(shù)排序并按標(biāo)簽分組的結(jié)果 * * 按CountVotes字段排序并按LabelName字段分組,返回count_voting表 */ public function getVotesGroupByLabel() { $sql = "SELECT * FROM count_voting ORDER BY LabelName DESC;"; // echo $sql; return $this->odb->query($sql); } } ?>
<?php /** * 頁面跳轉(zhuǎn)函數(shù) * 使用js實(shí)現(xiàn) * @param string $url */ function goToPgae($url) { echo "<script language='javascript' type='text/javascript'>"; echo "window.location.href='$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./ip/?q='.$ip; $content = file_get_contents($url); $preg = '/(?<=本站主數(shù)據(jù):<\/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; } ?>
然后就是后臺(tái)管理員的操作怎么弄了,主要是添加投票項(xiàng)的功能,操作數(shù)據(jù)庫上面已經(jīng)實(shí)現(xiàn)。后面的基本上是頁面怎么設(shè)置,關(guān)系到j(luò)s。添加投票項(xiàng)的頁面是動(dòng)態(tài)的,如下: function addVote() { right.innerHTML="<h2>添加投票項(xiàng)</h2>"; right.innerHTML+="<label>投票項(xiàng)標(biāo)簽<label>"; addInput("right","cLabelName","地區(qū)名"); right.innerHTML+="<br><label>投票項(xiàng)名稱<label>"; addInput("right","cSelectName","學(xué)校名"); 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) { //創(chuàng)建input var input = document.createElement("input"); input.type = "text"; input.id = id; input.placeholder = pla; document.getElementById(parent).appendChild(input); } <?php require_once '../api/func.php';
if (!isLoginNow()) { goToPgae("./index.php"); }
$name = $_GET["cSelectName"]; $label = $_GET["cLabelName"]; //echo $name."<br>".$label; require_once '../api/OperatorVotingDB.php'; $ovdb=new OperatorVotingDB(); $ovdb->addSelectName($name,$label); require './header.htm'; goToPgae("./admin.php?page=add&auto="."$label"."&id=cLabelName&foc=cSelectName&msg=添加成功"); ?>
//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; }
|