Limit the number of requests in the query

Asked

Viewed 100 times

0

Possuo is query that makes one request at a time, in random order.

$sql = "SELECT * FROM `viperusers` WHERE vip < 1 AND username != :username ORDER BY RAND() LIMIT 1";

Explaining, I select all my users, if it is my ignores, as you can see I limited to 1 for each Reload on the page catch a random user.

Every time you pass a user account +1 of mine Database, that is to each request sum +1, but I have a problem, I would like when selecting the same user not to do this count.

  • You can do it?

Here is the code:

public function counter($select_data, $following, $is_vip) {
  global $followersLimit, $follow;

  $followersAttempts = (int) $select_data[0]->count;

  if (($is_vip && $follow->_return->status == 1) 
        || !$is_vip) {
      $followersAttempts++;
  }

  if ($followersAttempts > $select_data[0]->count) {
    $this->_db->update('viperusers', array(
      'count' => $followersAttempts
    ), array(
      'username' => $select_data[0]->username
    ));
  }

  if ($followersAttempts >= $followersLimit) {
      $timer = ($is_vip) 
        ?$this->select_configs()[0]->timer_vip 
        :$this->select_configs()[0]->timer_free;

      $select_data[0]->timer = strtotime('+' . $timer . ' minutes');

      $this->_db->update('viperusers', array(
        'count' => 0,
        'timer' => $select_data[0]->timer
      ), array(
        'username' => $select_data[0]->username
      ));

      return json_encode(array(
        'follow'  => false,
        'timer'   => $timer
      ));
  }

  return json_encode(array(
    'follow'    => true,
    'followed'  => $select_data[0]->username,
    'following' => $following,
    'count'     => $followersAttempts
  ));
}

public function followers() {
  global $followersLimit, $follow;

  if (isset($_SERVER['REQUEST_METHOD']) 
      && $_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = Session::get('username');

    $select_data = $this->_requests->select_data($username);

    if ($select_data[0]->timer > time()) {
      echo json_encode(array(
        'follow'  => false,
        'timer'   => ceil((($select_data[0]->timer - time()) / 60))
      ));

      exit;
    }

    $select_random = $this->_requests->select_random($username);

    $follow = new Follow;

    $follow->setCookieFile(SMVC . 'application' . 
                              DS . 'cookies' . DS . $select_random[0]->username . '.txt');

    $follow->initialize( $select_data[0]->ds_user_id, 
        $select_random[0]->csrftoken );

    if ($follow->_return->status != $select_random[0]->status) {
      $this->_requests->set_status($select_random[0]->username, $follow->_return->status);
    }

    $followersLimit = $this->_requests->select_configs()[0]->followers_x;

    if ($this->_requests->select_is_vip($username)) {
      $is_vip = true;
      $followersLimit = 2 * $followersLimit;
    } else {
      $is_vip = false;
    }
    echo $this->_requests->counter($select_data, 
                                    $select_random[0]->username, $is_vip);
  }
}

Here is Vips check:

public function select_random($username) {
    if ($this->select_is_vip($username)) {
        $sql = "SELECT * FROM `viperusers` WHERE vip < 1 AND username != :username ORDER BY RAND() LIMIT 1";
    } else {
        $sql = "SELECT * FROM `viperusers` WHERE vip = 0 AND username != :username ORDER BY RAND() LIMIT 1";
    }

    $query = $this->_db->select($sql, array(
        ':username' => $username
    ));

    if (empty($query[0]->username)) {
        return $this->select_random($username);
    }

    return $query;
}
  • What to do and check if the user who was drawn is already being followed.

  • Yes, I thought about it, but how to do it? That’s the question I did it all but I stopped there

  • @That’s right, that’s right.

  • If it’s the same user, I don’t want it to count. Only the next user that hasn’t been selected.

  • @In other words, the user is active. If it is != of 1 he is not active, but he msm so selects and sums in the count. what wasn’t supposed to happen.

  • @I will edit the question with the controller code, and there is another query that asks the non-Vips as well.

  • @I edited it out.....

  • @I lost all sense, logic, I couldn’t make the check, so I came here to ask for a divine light.

  • And the non-Vips? same thing?

  • 1

    Vips need to have status == 1, non-Vips do not need

  • 1

    Or rather: if ( (($is_vip && $follow->_return->status == 1) || !$is_vip) && $followersAttempts == 0 ){

  • @And then stopped counting the selected ones but didn’t add the others. I’m trying to tidy up.

Show 7 more comments

1 answer

0

Some details I noticed: 1) I have not seen what the function select_is_vip does, but because you no longer keep if the user is vip here: 'username' => $select_data[0]->username, 'is_vip' => ($select_data[0]->vip==0?false:true) This avoids calling the select_is_vip function. 2) Where you are storing in the database this plus 1?

But if you do so, there will come a time when there will be no more users. For example, if in your database you have 10 users and you have 10 refreshs you will not have a user. I say this, because in your function you are running the risk of entering an infinite loop when this happens, because you have this command line: if (Empty($query[0]->username)) { Return $this->select_random($username); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.