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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?