How can I return all the users of a row and not just one?

0

How can I make it show all the usernames and the amount they carry? so far only the first user returns and the others do not ...

<?php

ini_set('display_errors', 1); error_reporting(E_ALL);


$ayer = new DateTime(''.date('Y-m-d', strtotime('last day')).'');
$ayer = $ayer->getTimestamp();

$hoy = new DateTime(date('Y-m-d'));
$hoy = $hoy->getTimestamp();

$link = new PDO('mysql:host=localhost;dbname=db', 'root', '12345678');
foreach ($link->query('SELECT * from members') as $row) { 
    $id_de_usuario= "". $row['id']. "\n";
    $username= ''. $row['username']. "\n";

foreach ($link->query("SELECT COUNT(*) total FROM ptsu_requests WHERE     status='Completed' AND date>=$ayer AND date<=$hoy AND user_id=" .     $id_de_usuario) as $row) { 

$fila1 = '' . $row['total'] . "</br>";

echo $fila1;


function setRankings($standings) {
    $rankings = array();
    arsort($standings);
    $rank = 1;
    $tie_rank = 0;
    $prev_score = -1;

    foreach ($standings as $name => $score) {
        if ($score != $prev_score) {  //this score is not a tie
                $count = 0;
            $prev_score = $score;
            $rankings[$name] = array('score' => $score, 'rank' => $rank);
        } else { //this score is a tie
            $prev_score = $score;
            if ($count++ == 0) {
            $tie_rank = $rank - 1;
        }
        $rankings[$name] = array('score' => $score, 'rank' => $tie_rank);
    }
    $rank++;
}
return $rankings;
}

//===================================================
//test the above function

$scores = array(
    $username => $fila1
);
$rankedScores = setRankings($scores);

//display the player rankings
foreach ($rankedScores as $player => $data) {
    echo $player . ' - ' . $data['score'] . ' - ' . $data['rank'] . '<br     />';
}
}
}



?>
    
asked by Luis Cesar 14.12.2018 в 03:09
source

1 answer

0

You should get the declaration of the function setRankings() out of the loop, otherwise it will give you an error of Fatal error: Can not redeclare setRankings () (previously declared in ...

I do not know why it is not shown or because you do not mention it, but it will give a sure error.

In addition, you should not use the same variable $row in two nested loops, even if they act in different areas they can fail or lead to confusion.

And in general, you should listen to A. Cedano and rethink how to structure the code.

Test:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

function setRankings($standings) {
    $rankings = array();
    arsort($standings);
    $rank = 1;
    $tie_rank = 0;
    $prev_score = -1;

    foreach ($standings as $name => $score) {
        if ($score != $prev_score) {  //this score is not a tie
            $count = 0;
            $prev_score = $score;
            $rankings[$name] = array('score' => $score, 'rank' => $rank);
        } else { //this score is a tie
            $prev_score = $score;
            if ($count++ == 0) {
                $tie_rank = $rank - 1;
            }
            $rankings[$name] = array('score' => $score, 'rank' => $tie_rank);
        }
        $rank++;
    }
    return $rankings;
}

$ayer = new DateTime('' . date('Y-m-d', strtotime('last day')) . '');
$ayer = $ayer->getTimestamp();

$hoy = new DateTime(date('Y-m-d'));
$hoy = $hoy->getTimestamp();

$link = new PDO('mysql:host=localhost;dbname=db', 'root', '12345678');

foreach ($link->query('SELECT * from members') as $row) {
    $id_de_usuario = "" . $row['id'] . "\n";
    $username = '' . $row['username'] . "\n";

    foreach ($link->query("SELECT COUNT(*) total FROM ptsu_requests WHERE     status='Completed' AND date>=$ayer AND date<=$hoy AND user_id=" . $id_de_usuario) as $row) {

        $fila1 = '' . $row['total'] . "</br>";

        echo $fila1;


//===================================================
//test the above function

        $scores = array(
            $username => $fila1
        );
        $rankedScores = setRankings($scores);

//display the player rankings
        foreach ($rankedScores as $player => $data) {
            echo $player . ' - ' . $data['score'] . ' - ' . $data['rank'] . '<br     />';
        }
    }
}
?>
    
answered by 14.12.2018 в 09:58