douqiong8412 2016-11-16 21:25
浏览 41
已采纳

PHP PDO MySQL计数登录条纹按日期

What I'm trying to do is:
* If user is logged on within 1 day, do nothing.
* if user is logged on after 1 day, and is within 2 days, set count + 1
* if user is logged on after 2 days, set count 1

$conn = $db->PDO();
try {
   $stmt = $conn->pdo->prepare("SELECT `id` FROM `user_logins` WHERE `user_id` = :uid LIMIT 1");
   $stmt->bindParam(':uid', $uid, $db->PARAM_INT);
   $stmt->execute();
   if($stmt->rowCount() > 0) {
      $stmt = $conn->pdo->prepare("SELECT `count`,`login_time` FROM `user_logins` WHERE `user_id` = :uid LIMIT 1");
      $stmt->bindParam(':uid', $uid, $db->PARAM_INT);
      $stmt->execute();
      $fetch = $stmt->fetch($db->FETCH_ASSOC);
      $loginStreak = $fetch['count'];
      $loginTime = $fetch['login_time'];
      $userPoints = $users->getUserInfo($uid, 'vip_points');
      if($loginTime < strtotime('+2 day')) {
          $stmt = $conn->pdo->prepare("UPDATE `user_logins` SET `count` = :c, `login_time` = :lt WHERE `user_id` = :uid LIMIT 1");
          $stmt->bindValue(':c', 1);
          $stmt->bindValue(':lt', NULL);
          $stmt->bindParam(':uid', $uid, $db->PARAM_INT);
          $stmt->execute();
       } elseif($loginTime < strtotime('+1 day')) {
          $stmt = $conn->pdo->prepare("UPDATE `user_logins` SET `count` = :c, `login_time` = :lt WHERE `user_id` = :uid LIMIT 1");
          if($loginStreak <= 7) {
            // login streak is lower or equal to 7 days, so give 1 point
            $users->updateUser($uid, 'vip_points', $userPoints + 1);
            $stmt->bindValue(':c', $loginStreak + 1);
          } elseif($loginStreak <= 14) {
            // login streak is lower or equal to 14 days, so give 2 points
            $users->updateUser($uid, 'vip_points', $userPoints + 2);
            $stmt->bindValue(':c', $loginStreak + 1);
          } elseif($loginStreak <= 21) {
            // login streak is lower or equal to 21 days, so give 3 points
            $users->updateUser($uid, 'vip_points', $userPoints + 3);
            $stmt->bindValue(':c', 21);
          }
          $stmt->bindValue(':lt', NULL);
          $stmt->bindParam(':uid', $uid, $db->PARAM_INT);
          $stmt->execute();
       }
     } else {
       $stmt = $conn->pdo->prepare("INSERT INTO `user_logins` (`user_id`,`login_time`,`count`) VALUES (:uid,:lt,:c)");
       $data = array(':uid' => $uid, ':lt' => NULL, ':c' => 1);
       $stmt->execute($data);
     } 
  } catch(PDOException $e) {
     die($e->getMessage());
  }
$conn = null;

So far I have come up with this, but it does not really work the way as written above. Any suggestions?

  • 写回答

1条回答 默认 最新

  • duanfangbunao36970 2016-11-16 21:41
    关注

    You can do it entirely in a single UPDATE query:

    UPDATE user_logins
    SET count = CASE 
            WHEN login_time > DATE_SUB(NOW(), INTERVAL 1 DAY)
                THEN count 
            WHEN login_time > DATE_SUB(NOW(), INTERVAL 2 DAY)
                THEN count + 1
            ELSE 1
        END,
        login_time = CASE 
            WHEN login_time <= DATE_SUB(NOW(), INTERVAL 1 DAY)
                THEN NULL
            ELSE login_time
        END
    WHERE user_id = :uid
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化