三、zend 控制器

三、zend 控制器

一、创建控制器

\application\controllers\BaseController.php

<?php
/**
 * Created by PhpStorm.
 * User: 52998
 * Date: 2019/6/2
 * Time: 14:03
 */
//做一个父类,专门供其他的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);

    }
}

后台控制器

application/controllers/AdminConrtoller.php

<?php

require_once 'BaseController.php';
class AdminConrtoller extends BaseController
{

    public function indexAction()
    {

    }


}

视图文件

application/views/scripts/admin/index.phtml

<h1>投票的后台管理程序</h1>

访问后台:域名/admin/index


增加选项页面

application/controllers/AdminController.php

<?php

require_once 'BaseController.php';
class AdminController extends BaseController
{

    public function indexAction()
    {
//        $this->render('index');
    }

    //进入增加选项的页面
    public function additemuiAction()
    {

    }
}

application/views/scripts/admin/additemui.phtml

<h1>增加投票选项</h1>
<form action="" method="post">
    <table>
        <tr>
            <td>name</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>描述</td>
            <td><textarea name="description" id="" cols="30" rows="10"></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>

设置模型application/models/Item.php

<?php
/**
 * Created by PhpStorm.
 * User: 52998
 * Date: 2019/6/2
 * Time: 15:11
 */
//这里必须继承Zend_Db_Table ,否则就不是表模型
class Item extends Zend_Db_Table
{
    protected $_name='item';  //设置表名
    protected $_primary = 'id';//设置主键id  默认为id
}

创建global 视图

application/views/scripts/globals/ok.phtml

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

控制器代码

application/controllers/AdminController.php

<?php

require_once APPLICATION_PATH.'/models/Item.php';
require_once 'BaseController.php';
class AdminController extends BaseController
{

    public function indexAction()
    {
//        $this->render('index');
    }

    //进入增加选项的页面
    public function additemuiAction()
    {

    }
    //完成一天假的任务
    public function additemAction()
    {
        $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();
        $inseId=$itemModel->insert($data);
        $this->render('ok');
    }
}


二、首页显示列表

控制器代码

application/controllers/IndexController.php

<?php
require_once APPLICATION_PATH.'/models/Item.php';
require_once 'BaseController.php';
class IndexController extends BaseController
{

    public function indexAction()
    {
        //创建一个item表模型
        $itemModel=new Item();

        $items = $itemModel->fetchAll()->toArray();
        $this->view->item=$items;
    }


}

视图代码

application/views/scripts/index/index.phtml

<h1>请投票</h1>
<table width="500px">
    <tr bgcolor="gray">
        <td>名称</td>
        <td>描述</td>
        <td>投票数</td>
        <td>投票数</td>
        <td>操作</td>
    </tr>
    <?php foreach ($this->items as $vo) {?>
    <tr>
        <td><?php echo $vo['id'];?></td>
        <td><?php echo $vo['name'];?></td>
        <td><?php echo $vo['description'];?></td>
        <td><?php echo $vo['vote_count'];?></td>
        <td><a href="">投票</a></td>
    </tr>
    <?php }?>
</table>

创建模型类

application/models/VoteLog.php

<?php
/**
 * Created by PhpStorm.
 * User: 52998
 * Date: 2019/6/2
 * Time: 15:55
 */

class VoteLog extends Zend_Db_Table
{
    protected $_name = 'vote_log';
    protected $_primary = 'id';

}

创建错误页面

application/views/scripts/vote/error.phtml

<script type="text/javascript">
    alert('你投过票了');
    history.back();
</script>

控制器

application/controllers/VoteController.php

<?php

require_once APPLICATION_PATH.'/models/Item.php';
require_once APPLICATION_PATH.'/models/VoteLog.php';
require_once 'BaseController.php';
class VoteController extends BaseController
{

    public function voteAction()
    {
        //获取用户投票的id
        $item_id=$this->getRequest()->getParam('itemid');
        $ip = $this->getRequest()->getServer('REMOTE_ADDR');

        $today = date('Ymd');

        //先看vote_log这个表中今天是否有记录
        $voteLogModel=new VoteLog();
        //sql注入先不考虑
        $where="ip='$ip' AND vote_date=$today";

        $res=$voteLogModel->fetchAll($where)->toArray();
        if (count($res)>5){
            //提示一句话
            $this->render('error');
            return ;
        }else{
            //更新item的vote_count,添加投票日志
            $data=array(
                'ip'=>$ip,
                'vote_date'=>$today,
                'item_id'=>$item_id
            );
            if ($voteLogModel->insert($data)>0){
                $newvote=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');

        }

    }


}






回复列表


回复操作