对应于控制器中的代码
//以下代码为学习代码
<?php
require_once 'BaseController.php';
require_once APPLICATION_PATH.'/models/Item.php';
require_once APPLICATION_PATH.'/models/VoteLog.php';
class VoteController extends BaseController{
public function voteAction(){
$item_id = $this->getRequest()->getParam('itemid','no'); //获取id
//用于获取ip 地址$_SERVER['REMOTE_ADDR']
//先看voteLOg 这个表今天有没有透过一次
$today=date('Ymd');//今天的时间
$voteLogModel = new VoteLog();
$where = "ip='$ip' AND vote_date=$today";
$res = $voteLogModel->fetchAll($where)->toArray();
if(count($res)>0){ //如果大于0表示已经投 了
$this->render('error');
return ;
}else{
//更新item的vote_count,添加更新日志
$data = array(
'ip' => $ip,
'vote_date'=>$today,
'item_id'=>$item_id
);
if($voteLogModel->insert($data)>0){ //如果更新成功要更改item 表
$itemModel = new Item();
//通过主键直接获取对应的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');
}
}
}
?>
本文介绍了一个投票控制器的具体实现过程,包括如何获取用户IP地址并检查今日是否已投票,以及如何更新被投票项目的计数器。
5191

被折叠的 条评论
为什么被折叠?



