09-php框架编写的项目--投票系统

来源:互联网 发布:usb3.0根集线器 ubuntu 编辑:程序博客网 时间:2024/06/07 15:57

需求:

1.用户访问votesys.com可以显示投票的项目的列表信息,可以进行投票,可以限制投票的次数。

2.后台可以对投票的项目进行增加,增加的时候有验证(服务器的验证)

3.后台可以添加禁用的IP,被禁用的Ip不可以进行投票。


创建数据库:votedb

数据库表的创建:

--选项表,MyISAM和InnoDB区别是InnoDB有外键,MyISAM运行快。create table item(--选项编号id bigint unsigned primary key auto_increment,--选项名称name varchar(64) not null,--选项描述description varchar(128) not null,--投票次数vote_count bigint unsigned)engine MyISAM;--投票日志表create table vote_log(--投票日志IDid bigint unsigned primary key auto_increment,--投票者IP地址ip varchar(20) not null,--投票者日期vote_date bigint not null,--对哪个投的item_id bigint not null)engine MyISAM;--过滤IP表create table filter(id bigint unsigned primary key auto_increment,ip varchar(20))engine MyISAM;


在DOS创建项目:
zf.bat create project d:/myvotesys
在zs中创建空项目votesys并copy已经创建好的项目文件。



然后在hosts文件中加入:

127.0.0.1  votesys.com   



然后配置虚拟主机:

在httpd-vhosts.conf文件中加入

#投票系统
<VirtualHost *:80>
    DocumentRoot "C:/myenv/apache/htdocs/votesys/public"
    ServerName votesys.com
    DirectoryIndex index.php
    <Directory />
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    </Directory>
</VirtualHost>


application.ini

[production]phpSettings.display_startup_errors = 0phpSettings.display_errors = 0includePaths.library = APPLICATION_PATH "/../library"bootstrap.path = APPLICATION_PATH "/Bootstrap.php"bootstrap.class = "Bootstrap"appnamespace = "Application"resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"resources.frontController.params.displayExceptions = 0[mysql]db.adapter=PDO_MYSQLdb.params.host=localhostdb.params.username=rootdb.params.password=rootdb.params.dbname=votedb

AdminController.php

<?php//如果文件在同一级目录下,直接写入文件名称引入即可//如果文件不再同一级的目录下面,需要用绝对路径进行引入require_once 'BaseController.php';require_once APPLICATION_PATH . '/models/Item.php';require_once APPLICATION_PATH . '/models/Filter.php';/** *  * @author Administrator *这个控制器专门用来管理后台 */class AdminController extends BaseController {/** * 跳转到index.php页面的Action * index.php是首页显示头片项目列表 * 以及投票的投票次数,投票操作的主页面 */public function indexAction() {// action body}/** * 进入到后台增加投票选项的页面 *用于添加需要投票的项目 *如苹果,鸭梨......  */public function additemuiAction() {//如果action中什么都没有写,相当于$this->render ( 'additemui' );}/** * 处理投票项目添加的请求 * 通过这个方法可以向数据库中的item表中插入 *新的投票项目 */public function additemAction() {//获取用户输入的内容$name = $this->getRequest ()->getParam ( 'name' );if ($name == "") {$this->view->info = '您输入的名字为空';//显示信息$this->_forward ( 'error', 'global' );return;}$description = $this->getRequest ()->getParam ( 'description' );//$vote_count = $this->getRequest ()->getParam ( 'vote_count' );//使用$vote_count = empty ( $_REQUEST ['vote_count'] ) ? 0 : $_REQUEST ['vote_count'];//在服务器对输入的数据进行验证//echo $name.'--'.$description.'--'.$vote_count;//exit();//数组$data = array ('name' => $name, 'description' => $description, 'vote_count' => $vote_count );//创建一个表模型对象$itemModel = new Item ();$itemModel->insert ( $data );//插入数据记录成功,跳转到$this->render ( 'ok' );}/** * 用于处理增加过滤ip超链接请求的action * 通过render跳转到添加过滤ip页面 */public function addfilteripuiAction() {$this->render ( 'addfilterip' );}/** * 响应增加ip的请求 * 用于向数据库中添加一个封杀的ip地址 */public function addfilteripAction() {//获取用户输入的ip是多少$ip = $this->getRequest ()->getParam ( 'ip' );$filterModel = new Filter ();//添加到数据库中一个记录$data = array ('ip' => $ip );if ($filterModel->insert ( $data ) > 0) {//说明在数据库中添加数据成功,跳转到全局视图//跳转到globalController下的okAction方法对应的视图ok.phtml//添加返回的信息$this->view->info = '增加过滤IP成功!';$this->_forward ( 'ok', 'global' );} else {$this->view->info = '增加过滤IP失败!';$this->_forward ( 'error', 'global' );}}}

BaseController.php

<?php//做一个父类,专门供其它的controller来继承class BaseController 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 );}}?>

GlobalController.php

<?phprequire_once 'BaseController.php';/** *  * @author Administrator */class GlobalController extends BaseController {//跳转到ok界面public function okAction() {}//跳转到error界面public function errorAction() {}}
IndexController.php

<?phprequire_once 'BaseController.php';require_once APPLICATION_PATH . '/models/Item.php';class IndexController extends BaseController {public function indexAction() {//创建一个item表模型$itemModel = new Item ();$items = $itemModel->fetchAll ()->toArray ();//echo "<pre>";//print_r($items);//echo "</pre>";//exit();$this->view->items = $items;}}

VoteController.php

<?phprequire_once 'BaseController.php';require_once APPLICATION_PATH . '/models/Item.php';require_once APPLICATION_PATH . '/models/VoteLog.php';require_once APPLICATION_PATH . '/models/Filter.php';/** *  * @author Administrator *这个控制器专门用来处理投票请求 */class VoteController extends BaseController {/** * 跳转到index.php页面的Action */public function voteAction() {//echo "oooook";//exit();//获取用户投票的Id$item_id = $this->getRequest ()->getParam ( 'itemid', 'no' );//获取ip地址//$_SERVER ['REMOTE_ADDR'];$ip = $this->getRequest ()->getServer ( 'REMOTE_ADDR' );//看看该用户的ip是否被禁用$filterModel=new Filter();$filters=$filterModel->fetchAll("ip='$ip'")->toArray();if(count($filters)>=1){$this->view->info='您的IP被禁用了!您不能投票了!';   $this->_forward('error','global');   return ;}//echo $item_id . "--" . $ip;//exit();//获取当前日期$today = date ( 'Ymd' );//先看看vote_log表中是否投过一次//创建votelog实例$voteLogModel = new VoteLog ();//条件语句$where = "ip='$ip' AND vote_date=$today";$res = $voteLogModel->fetchAll ( $where )->toArray ();if (count ( $res ) > 5) {//提示一句话$this->render ( 'error' );return;} else {//更新vote_count,添加投票日志$data = array ('ip' => $ip, 'vote_date' => $today, 'item_id' => $item_id );if ($voteLogModel->insert ( $data ) > 0) {//如果返回的数值大于0,添加成功//更新$itemModel = new Item ();//通过主键获取对应的记录$item = $itemModel->find ( $item_id )->toArray ();$newvote = $item [0] ['vote_count'] + 1;$set = array ('vote_count' => $newvote );$where = "id=$item_id";$itemModel->update ( $set, $where );}$this->render ( 'ok' );}}}


Filter.php

<?php/** * 这里必须继承zend_db_table 否则就不是表模型 */class Filter extends Zend_Db_Table {//默认的主键为IDprotected $_name = 'filter';}?>

Item.php

<?php/** * 这里必须继承zend_db_table 否则就不是表模型 */class Item extends Zend_Db_Table {//默认的主键为IDprotected $_name = 'item';}?>
VoteLog.php

<?php/** * 这里必须继承zend_db_table 否则就不是表模型 */class VoteLog extends Zend_Db_Table {//默认的主键为ID,设置和对应的表关联protected $_name = 'vote_log';}?>


addfilterip.phtml

<h1>增加过滤ip</h1><form action='/admin/addfilterip' method='post'><table><tr><td>ip地址</td><td><input type='text' name='ip' /></td></tr><tr><td><input type='submit' value='增加' /></td><td><input type='reset' value='重填' /></td></tr></table></form>
additemui.phtml

<h1>增加投票选项</h1><form action='/admin/additem' method='post'><table><tr><td>名称</td><td><input type='text' name='name' /></td></tr><tr><td>描述</td><td><textarea rows="5" cols="30" name="description"></textarea></td></tr><tr><td>初始化投票数</td><td><input type='text' name='vote_count' /></td></tr><tr><td><input type='submit' value='增加' /></td><td><input type='reset' value='重填' /></td></tr></table></form>

index.phtml

<h1>投票的后台管理程序</h1><ul><li><a href='/admin/additemui'>增加投票选项</a></li><li><a href="/admin/addfilteripui">增加过滤IP</a></li></ul>


ok.phtml

<script type="text/javascript">alert('投票操作成功!');history.back();</script>

error.phtml

<script type="text/javascript">alert('<?phpecho $this->info?>');history.back();</script>

ok.phtml

<script type="text/javascript">alert('<?phpecho $this->info?>');history.back();</script>

index.phtml

<link href="/css/mycss.css" type="text/css" rel="stylesheet" /><script type="text/javascript" src="/js/myjs.js"></script><h1>请你投票</h1><div id="img"><img src="/images/logo.gif" width="640px" height="480px" /></div><table border="1" cellspacing="0" bordercolor="green" width="500px"height="200px"><tr bgcolor="yellow"><td>name</td><td>description</td><td>vote_count</td><td>vote</td></tr><?phpforeach ( $this->items as $item ) {?><tr><td><?phpecho $item ["name"]?></td><td><?phpecho $item ['description']?></td><td><?phpecho $item ['vote_count']?></td><td><a href="/vote/vote?itemid=<?phpecho $item ['id']?>">投票</a></td></tr><?php}?></table>

error.phtml

<script type="text/javascript">alert('您今天投票的次数已经木有了,不能再进行投票了^v^~');history.back();</script>


ok.phtml

<script type="text/javascript">alert('投票操作成功!');history.back();</script>

目录截图:

文件按照目录的顺序书写。


访问后台:


超级个性的主页面:



项目资源文件下载地址:http://download.csdn.net/detail/u010653050/5977835



原创粉丝点击