建立項目
1.cmd內進入ZendFramework內的bin用命令新建zf項目
zf.bat create project G:/xampp/htdocs/VoteSys
2.將Zend文件放入新建項目的library
設置虛擬主機
1.xampp\apache\conf\extra\httpd-vhosts.conf內添加
<VirtualHost *:8081>
ServerAdmin VoteSys.com
DocumentRoot "/xampp/htdocs/VoteSys/public"
ServerName VoteSys.com
DirectoryIndex index.php
<Directory "/xampp/htdocs/VoteSys/public">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog "logs/VoteSys-error.log"
CustomLog "logs/VoteSys-access.log" common
</VirtualHost>
2.C:\Windows\System32\drivers\etc\hosts內配置
127.0.0.1 VoteSys.com
建立數據庫及表格
1.item表
DROP TABLE IF EXISTS `item`;
CREATE TABLE `item` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`description` varchar(128) NOT NULL,
`vote_count` bigint(20) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
2.vote_log表
DROP TABLE IF EXISTS `vote_log`;
CREATE TABLE `vote_log` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`ip` varchar(20) NOT NULL,
`vote_date` bigint(20) NOT NULL,
`item_id` bigint(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3.filter表
DROP TABLE IF EXISTS `filter`;
CREATE TABLE `filter` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`ip` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
設置VoteSys內的數據庫連接信息
1.htdocs\VoteSys\application\configs\application.ini內配置
[mysql]
db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = admin
db.params.password = admin
db.params.dbname = test
2.新建\htdocs\VoteSys\application\controllers\DBController.php
class DBController extends Zend_Controller_Action
{
public function init()
{
//初始化數據庫適配器
$url = constant("APPLICATION_PATH")
.DIRECTORY_SEPARATOR.'configs'
.DIRECTORY_SEPARATOR.'application.ini';
$dbconfig = new Zend_Config_Ini($url, "mysql");
$db = Zend_Db::factory($dbconfig->db);
$db -> query('SET NAMES UTF8');
Zend_Db_Table::setDefaultAdapter($db);
}
}
3.爲每個表格設置一個models
3.1 item表
\htdocs\VoteSys\application\models\item.php
class item extends Zend_Db_Table
{
protected $_name = 'item';
protected $_primary = 'id';
}
3.2 vote_log表
class vote_log extends Zend_Db_Table
{
protected $_name = 'vote_log';
}
3.3 filter表
class filter extends Zend_Db_Table
{
protected $_name = 'filter';
}
後臺首頁設置
1.新建後臺控制器
\htdocs\VoteSys\application\controllers\AdminController.php
require_once 'DBController.php';
class AdminController extends DBController
{
public function indexAction() {
# code...
}
}
2.新建後臺首頁視圖
\htdocs\VoteSys\application\views\scripts\admin\index.phtml
<ul>
<li><a href="/admin/additem">投票</a></li>
<li><a href="/admin/filter">過濾IP</a></li>
</ul>
後臺添加投票項
1.顯示增加選擇項頁面
\htdocs\VoteSys\application\controllers\AdminController.php添加additemAction
// 增加選擇項頁面
public function additemAction() {
# code...
}
2.添加增加選項頁面視圖
\htdocs\VoteSys\application\views\scripts\admin\additem.phtml
<form action="/admin/additemfunc" method="post">
<table>
<tr>
<td>name</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>描述</td>
<td>
<textarea name="description" cols="30" rows="5"></textarea>
</td>
</tr>
<tr>
<td>初始化投票數</td>
<td>
<input type="text" name="vote_count"></input>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit"></input>
</td>
<td>
<input type="reset" value="Reset" />
</td>
</tr>
</table>
</form>
3.添加選擇項後臺處理
在AdminController.php內新增additemfuncAction
require_once APPLICATION_PATH.'/models/item.php';
// 完成添加任務
public function additemfuncAction() {
//獲取用戶輸入內容
$name = $this->getRequest()->getParam('name');
$description = $this->getRequest()->getParam('description');
$vote_count = $this->getRequest()->getParam('vote_count');
$data = array(
'name' => $name,
'description' => $description,
'vote_count' => $vote_count
);
//創建一個表格模型
$itemModel = new item();
$itemModel -> insert($data);
//頁面跳轉
$this->render('success');
}
4.添加成功視圖
\htdocs\VoteSys\application\views\scripts\admin\success.phtml
<script type="text/javascript">
alert('success!');
history.back();
</script>
過濾IP設置
1.添加過濾頁面
AdminController.php內添加
// 過濾頁面
public function filterAction() {
# code...
}
2.新建過濾頁面視圖
\htdocs\VoteSys\application\views\scripts\admin\filter.phtml
<form action="/admin/filterfunc" method="post">
<table>
<tr>
<td>IP</td>
<td>
<input type="text" name="ip" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit"></input>
</td>
<td>
<input type="reset" value="Reset" />
</td>
</tr>
</table>
</form>
3.過濾處理
AdminController.php內添加
require_once APPLICATION_PATH.'/models/filter.php';
// 完成過濾任務
public function filterfuncAction() {
//獲取用戶輸入信息
$ip = $this->getRequest()->getParam('ip');
$data = array(
'ip' => $ip
);
//創建一個表模型
$filterModel = new filter();
//全局頁面跳轉
if ($filterModel->insert($data) > 0) {
//成功 跳轉全局視圖
//挑戰至GlobalController內地okAction 對應視圖
$this->view->info = "IP $ip 過濾成功";
$this->forward('ok', 'Global');
} else {
//失敗
$this->view->info = "IP $ip 過濾失敗";
$this->forward('error', 'Global');
}
}
4.全局反饋
1.建立全局控制器
\htdocs\VoteSys\application\controllers\GlobalController.php
class GlobalController extends Zend_Controller_Action
{
//跳轉ok界面
public function okAction() {
}
//失敗error界面
public function errorAction() {
}
}
2.建立全局視圖
新建
\htdocs\VoteSys\application\views\scripts\global
1.成功視圖
新建
\htdocs\VoteSys\application\views\scripts\global\ok.phtml
<script type="text/javascript">
alert('<?php echo $this->info; ?>');
history.back();
//返回並刷新
//window.location.href="/index/index";
</script>
2.失敗視圖
新建
\htdocs\VoteSys\application\views\scripts\global\error.phtml
<script type="text/javascript">
alert('<?php echo $this->info; ?>');
history.back();
</script>
前臺控制
1.前臺首頁顯示
\htdocs\VoteSys\application\controllers\IndexController.php
require_once 'DBController.php';
require_once APPLICATION_PATH.'/models/item.php';
class IndexController extends DBController
{
public function indexAction()
{
// 創建表模型
$itemModel = new item();
$items = $itemModel->fetchAll()->toArray();
$this->view->items = $items;
}
}
2.添加首頁視圖
\htdocs\VoteSys\application\views\scripts\index\index.phtml
<table width="500px" align="center">
<tr bgcolor="#deb887" align="center">
<td>名稱</td>
<td>描述</td>
<td>票數</td>
<td>投票</td>
</tr>
<?php foreach ($this->items as $item) {?>
<tr align="center">
<?php echo "<td>".$item['name']."</td>
<td>".$item['description']."</td>
<td>".$item['vote_count']."</td>";?>
<td>
<a href="/vote/vote?id=<?php echo $item['id']; ?>">
投票
</a>
</td>
</tr>
<?php } ?>
</table>
3.投票處理
新建\htdocs\VoteSys\application\controllers\VoteController.php
require_once 'DBController.php';
require_once APPLICATION_PATH.'/models/item.php';
require_once APPLICATION_PATH.'/models/vote_log.php';
require_once APPLICATION_PATH.'/models/filter.php';
class VoteController extends DBController
{
public function voteAction() {
//獲取用戶投票ID
$id = $this->getRequest()->getParam('id');
$ip = $this->getRequest()->getServer('REMOTE_ADDR');
//查看vote_log今天是否已投票
$votelogModel = new vote_log();
$today = date('Ymd');
$where = "ip = '$ip' AND vote_date = $today";
$res = $votelogModel -> fetchAll($where) -> toArray();
//查看該ip是否可以投票
$filterModel = new filter();
$where = "ip = '$ip'";
$resFilter = $filterModel -> fetchAll($where) -> toArray();
if(count($res) > 5 ) {
//提示已投票
$this->render('error');
return ;
} else if(count($resFilter) > 0) {
//該IP被過濾
$this->render('errorfilter');
return ;
} else {
//投票
//添加投票日誌
$data = array(
'ip' => $ip,
'vote_date' => $today,
'item_id' => $id
);
//更新
if($votelogModel -> insert($data) > 0) {
$itemModel = new item();
//通過組件獲取對應的item
$item = $itemModel -> find($id) -> toArray();
$newvote = $item[0]['vote_count']+1;
$set = array(
'vote_count' => $newvote
);
$where = "id = $id";
$itemModel -> update($set, $where);
}
$this->render('success');
}
}
}
4.投票成功視圖
新建\htdocs\VoteSys\application\views\scripts\vote\success.phtml
<script type="text/javascript">
alert('success!');
history.back();
</script>
5.已投票視圖
新建\htdocs\VoteSys\application\views\scripts\vote\error.phtml
<script type="text/javascript">
alert('已投票!');
history.back();
</script>
6.IP被過濾視圖
新建\htdocs\VoteSys\application\views\scripts\vote\errorfilter.phtml
<script type="text/javascript">
alert('該IP被禁止投票!');
history.back();
</script>
引入圖片、js、css
1.新建IMAGES、JS、CSS文件夾
\htdocs\VoteSys\public\IMAGES
\htdocs\VoteSys\public\JS
\htdocs\VoteSys\public\CSS
2.新建IMAGES、JS、CSS文件夾內文件
\htdocs\VoteSys\public\IMAGES\1.jpg
\htdocs\VoteSys\public\JS\test.js
\htdocs\VoteSys\public\CSS\test.css
3.引用IMAGES、JS、CSS文件夾內文件
<img src="/images/1.jpg" alt="圖片" />
<script type="text/javascript" src="/js/test.js"></script>
<link type="text/css" rel="stylesheet" href="/CSS/test.css">