How can I save the maximum value obtained in rowCount?

1

How do I logically only update my variable when the value of RowCount is the largest one ever obtained?

Example: I did the sql query and returned 15 records, saved 15 to the variable $maximo so I made another query and returned 12 so I do not update the variable $maximo , I made a third query and returned 17 then I update the variable $maximo to 17.

Current status:

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'm not sure how to start the maximum variable.

    
asked by anonymous 17.12.2017 / 15:17

1 answer

0

There are a few things to consider. Will multiple accesses to the playeronline() function be done on the same http request, or on different requestions from different users? Let's see an example for each case.

Multiple hits on 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 on different requests

Here you can use the global variable $_SESSION to persuade data between different http (of pages) requests. Then it stays:

<?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();
    }
}
    
17.12.2017 / 16:19