基于TP的每日签到代码

  1. // 每日签到
  2.         public function dailySign(){
  3.             // 获取用户签到信息
  4.             $merArr = M('merchant') -> where("MerID = '$this->UserId'") -> find();
  5.             // 判断是否为注册后第一次签到
  6.             if($merArr['totalTime'] == 0 && $merArr['totalnum'] == 0){
  7.                 // 获取签到设置
  8.                 $signArr = M('sign') -> where("num = 1") -> find();
  9.                 $Yunbi = $signArr['yun'];
  10.                 $score['Mertotal'] = array('exp',"Mertotal + $Yunbi");
  11.                 $score['totalnum'] = 1;
  12.                 $score['totalTime'] = time();
  13.                 $result = M('merchant') -> where("MerID = '$this->UserId'") -> save($score);
  14.                 if($result > 0){
  15.                     echo "签到成功";
  16.                 }else{
  17.                     echo "签到失败";
  18.                 }
  19.             }else{
  20.                 // 获取登录用户签到信息
  21.                 $totalTime = $merArr['totalTime']; // 签到时间
  22.                 $totalnum = $merArr['totalnum']; // 签到次数
  23.                 $signTime = date('y-m-d',$totalTime); // 格式化签到时间
  24.                 // 判断今天是否已经签到
  25.                 if($signTime == date('y-m-d',time())){
  26.                     echo "已签到";
  27.                     exit;
  28.                 }else{
  29.                     // 判断是否错过连续签到时间
  30.                     if(time() - $totalTime > 60*60*24){
  31.                         // 获取签到设置
  32.                         $signArr = M('sign') -> where("num = 1") -> find();
  33.                         $Yunbi = $signArr['yun'];
  34.                         $score['Mertotal'] = array('exp',"Mertotal + $Yunbi");
  35.                         $score['totalnum'] = 1;
  36.                         $score['totalTime'] = time();
  37.                         $result = M('merchant') -> where("MerID = '$this->UserId'") -> save($score);
  38.                         if($result > 0){
  39.                             echo "签到成功";
  40.                         }else{
  41.                             echo "签到失败";
  42.                         }
  43.                     }else{
  44.                         // 获取签到设置
  45.                         $signArr = M('sign') -> where("num = $totalnum + 1") -> find();
  46.                         if($signArr != null){
  47.                             $Yunbi = $signArr['yun'];
  48.                         }else{
  49.                             $maxYun = M('sign') -> Max('num');
  50.                             $signArr = M('sign') -> where("num = $maxYun") -> find();
  51.                             $Yunbi = $signArr['yun'];
  52.                         }
  53.                         $score['Mertotal'] = array('exp',"Mertotal + $Yunbi");
  54.                         $score['totalnum'] = $totalnum + 1;
  55.                         $score['totalTime'] = time();
  56.                         $result = M('merchant') -> where("MerID = '$this->UserId'") -> save($score);
  57.                         if($result > 0){
  58.                             echo "签到成功";
  59.                         }else{
  60.                             echo "签到失败";
  61.                         }
  62.                     }    
  63.                 }
  64.             }
  65.         }
  1. --
  2. -- 表的结构 `web_sign`
  3. --
  4. CREATE TABLE IF NOT EXISTS `web_sign` (
  5.   `id` int(11) NOT NULL AUTO_INCREMENT,
  6.   `num` tinyint(4) NOT NULL,
  7.   `yun` int(11) NOT NULL,
  8.   PRIMARY KEY (`id`)
  9. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COMMENT='签到表' AUTO_INCREMENT=3 ;
  10. --
  11. -- 转存表中的数据 `web_sign`
  12. --
  13. INSERT INTO `web_sign` (`id`, `num`, `yun`) VALUES
  14. (1, 1, 20),
  15. (2, 2, 40);

你可能感兴趣的:(php)