ZendFramework小例子——投票

建立项目

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">
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值