Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
277 views
in Technique[技术] by (71.8m points)

php - Sum values in each group with a loop

I have a while loop that gives this result:

Userid      Point
1           10
1           15
2           5
2           10
3           8
3           2

How can I sum the userid points and output with highest number first, like this:

Userid     Point
1          25
2          20
3          10

Is there any "foreach", "for" or any other method that can accomplish such result?

The code:

include ('variables.php');

//Fetch data from matchdata table
$q = "SELECT userid, matchid, homescore, awayscore FROM predictiondata ORDER BY userid ASC";
$r = mysqli_query($mysqli, $q);

while ($row = mysqli_fetch_array($r)) {
    //Define predictions
    $predhome = $row['homescore'];
    $predaway = $row['awayscore'];

    //Fetch gameresults
    $qres = "SELECT id, homescore, awayscore, bonuspoints FROM matches WHERE id = ".$row['matchid']."";

    $rres = mysqli_query($mysqli, $qres);
    $result = mysqli_fetch_array($rres);
    $homescore = $result['homescore'];
    $awayscore = $result['awayscore'];
    $bonus = $result['bonuspoints'];
    $id = $result['id'];



    //Calculate points
            if ($homescore == $predhome && $awayscore == $predaway && $homescore != '') { $result_point = $correct_score + $correct_result + $correct_number + $correct_number; } 
            else if ($homescore == $predhome && $awayscore != $predaway OR $homescore != $predhome && $awayscore == $predaway) { $result_point = $correct_result + $correct_number; } 
            else if ($predhome > $predaway && $homescore > $awayscore OR $predhome < $predaway && $homescore < $awayscore) { $result_point = $correct_result; } 
            else if (is_null($predhome) OR $homescore == '') { $result_point = 0; } 
            else { $result_point = 0; }

            if ($homescore == $predhome && $awayscore == $predaway && $homescore != '') { $bonus = $bonus; } 
            else if (is_null($predhome) OR $homescore == '') { $bonus = 0;  } 
            else { $bonus = 0;  }

            if (is_null($predhome) OR $homescore == '') { $total_point = 0; } 
            else { $total_point = $result_point + $bonus; } 


            //Calculate total round sum

            $total_roundsum = $result_point + $bonus;
            //echo $username.' - '.$total_roundsum.'<br />';

    if($total_roundsum != 0) {
    echo $row['userid']. ' - ' .$total_roundsum.'<br />';
    }
}

At the moment, the code only echo's the results. The "variables.php" holds the $correct_score + $correct_result + $correct_number variables.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming the two columns are an associated array -- $users_and_points.

$points_array = array();
foreach ( $users_and_points as $user_id => $point ) {
    if( !isset( $points_array[$user_id] ) {
        $points_array[$user_id] = 0;
    }
    $points_array[$user_id] += $point;
}
// newly associated array with calculated totals
$points_array;

Update

Based on your code above instead of echoing build an array of users and points.

echo $row['userid']. ' - ' .$total_roundsum.'<br />';

can be replaced with:

if( !isset( $points_array[$row['userid']] ) {
    $points_array[$row['userid']] = 0;
}
$points_array[$row['userid']] += $total_roundsum;

That will give you an associated array of user ids and associated points.

print_r( $points_array );

Note: Set the variable before your loop. Example, $points_array = array();


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...