How can I save the maximum value obtained in rowCount?

Asked

Viewed 34 times

1

How do I logica so that only update my variable when the Rowcount value is the highest ever obtained?

Example: I performed the sql query and returned 15 record, saved 15 in variable $maximothen I performed another query and returned 12 so I do not update the variable $maximo, performed a third query and returned 17 then i update the variable $maximo to 17.

Current condition:

static function playeronline($pdoG)
{
    try {
        $playeronline = $pdoG->prepare("SELECT login FROM u_hero WHERE login = 1");
        $playeronline->execute();

        $numeroonline = $playeronline->rowCount();
        $numeroonline = $numeroonline * 1.3;
        $numeroonline = round($numeroonline);

        return $numeroonline;



    } catch (PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}

I don’t know how to start the maximum variable.

  • Just check if the returned value is greater than what you have in the variable $maximo, but, it has how to put its code?

  • So I don’t know how to start the maximum variable, I will change and put the current code.

1 answer

0

There are some things to consider. The multiple accesses to the function playeronline() will be made in the same http request, or in different requests from different users? Let’s see an example for each case.

Multiple accesses in the same request

<?php
$maximo = 0;

//um loop para simular varias chamadas à função `playeronline`
for($i = 0; i < 5; i++){
    $players = playeronline(databaseConnection);
    if($players > $maximo){
        $maximo = $players;
    }
}

//depois faça alguma coisa com a variavel $maximo

static function playeronline($pdoG)
{
    try {
        $playeronline = $pdoG->prepare("SELECT login FROM u_hero WHERE login = 1");
        $playeronline->execute();

        $numeroonline = $playeronline->rowCount();
        $numeroonline = $numeroonline * 1.3;
        $numeroonline = round($numeroonline);

        return $numeroonline;



    } catch (PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}

Multiple accesses in different requests

Here you can use the global variable $_SESSION to scan data between http (page) requests. Then:

<?php
//inicializa o uso de sessão (retire se você já estiver incluindo isso)
session_start();

//inicializa $_SESSION['maximo'] com 0 no primeiro acesso
if(!isset($_SESSION['maximo'])){
    $_SESSION['maximo'] = 0;
}

$players = playeronline(databaseConnection);

if($players > $_SESSION['maximo']){
    $_SESSION['maximo'] = $players;
}

//depois faça alguma coisa com a variavel $maximo

static function playeronline($pdoG)
{
    try {
        $playeronline = $pdoG->prepare("SELECT login FROM u_hero WHERE login = 1");
        $playeronline->execute();

        $numeroonline = $playeronline->rowCount();
        $numeroonline = $numeroonline * 1.3;
        $numeroonline = round($numeroonline);

        return $numeroonline;



    } catch (PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
}
  • If I initialize the variable $maximo = 0; outside of Function every time I call the file it will not reset no?

  • Yes will reset. In this case you should use the second example, where the variable $_SESSION[$maximo] will only be initialized on the first inclusion (called from the file).

  • Does Session not boot for every access to the site? type if a user logs in? or the server itself already starts a Session?

  • Session is initialized when a user accesses the site (first access), until the session is closed, the data is not erased between requests. Back to your case, each user in the first access will have $_SESSION['maximo'] initialized with 0, but in the later accesses will be "remembered" the last value of rowCount.

  • But then if a user never accessed my site the maximum would be 0 and would not be profitable since I want users to know which was the largest amount of hits.

  • If he has never accessed your site (never seen your site) the zero value does not apply. Whenever there is an access (first) the value will no longer be zero. But I have a doubt, it seems to me that you do the calculation based on an sql query, and apparently keep it in session seems to be unnecessary. This proceeds?

  • Yes because whenever someone enters I perform the consultation of plays online and have to keep registered a maximum value, I will make an insert and keep checking the value and update it if it is greater than already obtained, was the solution.

Show 2 more comments

Browser other questions tagged

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