建立项目
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">