Calculate how many times a champion was picked | PHP | LARAVEL

  api, count, laravel, php

I’m trying to calculate how many times a champion was selected coming from my database ,
but the data i’m getting is not correct , for example leona was picked 6 times and had 3 victories in general for every team , but im using a select so if i select TSM for example , tsm picked leona 3 times with 1 Victory

but what i’m gettin when i select tsm is , leona picked 4 times with 3 victories 75% winrate(image)

so how can i make this work and get the expected result (leona picked 3 times , 1 victory , 33,3% winrate)

<?php
        // $team is coming from a select , example: I selected TSM  and press search , so the code  go to my db and search where tsm appears

 $db = DB::table("games")->select("picksBlueSide", "picksRedSide", "sideWin", "winner",'team','vsteam')->where("vsteam", $team)->get();


    $heroPicks = array();
    $winrate = array();
    $winrate2 = array();
    $picks = array();
    $countPicks = array();
    $picks1 = array();
    $picks2 = array();
    foreach ($db as $data) {
        //$data->winner and $data->sideWin are records coming from db


        // if winner is TSM (for example) and  sideWin = BLUE i know tsm won blue side and i get all the games with this condition 
        switch ([$data->winner, $data->sideWin]) {
            case ([$team, "Blue"]):
                $picks1 = json_decode($data->picksBlueSide);
                $countPicks = $picks1;
                echo "TSM WON Blueside ";
            break;
        }
        // if winner is TSM (for example) and  sideWin = Red i know tsm Red side
        switch ([$data->winner, $data->sideWin]) {
            case ([$team, "Red"]):
                $picks2 = json_decode($data->picksRedSide);
                $countPicks = $picks2;
                echo "TSM WON RedSide ";
            break;
     
        // if winner is not TSM (for example) and  sideWin = BLUE i know tsm lost red side and played redside
       
            case ([$data->winner != $team, "Red"]):
                $picks1 = json_decode($data->picksBlueSide);
                $countPicks = $picks1;
                echo "Tsm lost Redside ";
            break;
        
        // if winner is not TSM (for example) and  sideWin = Red i know tsm lost  blue side and played blueside
      
            case ([$data->winner != $team, "Blue"]):
                $picks2 = json_decode($data->picksRedSide);
                $countPicks = $picks2;
                echo "Tsm lost Blueside ";
            break;
        }
        $picks = array_merge($picks1, $picks2);

    //the  code below  is to make the calculation of times a champion was selected
            foreach ($picks as $pick) {
                $pick = trim($pick);
                if (!array_key_exists($pick, $heroPicks)) {
                    $heroPicks[$pick] = 0;
                }
                $heroPicks[$pick]+= 1;
            }
            foreach ($countPicks as $picka) {
                $picka = trim($picka);
                if (!array_key_exists($picka, $winrate)) {
                    $winrate[$picka] = 0;
                }
                $winrate[$picka]+= 1;
            }
        }
        uasort($heroPicks, function ($a, $b) {
            return $a - $b;
        });
        uasort($winrate, function ($c, $d) {
            return $c - $d;
        });
        $heroPicks = array_reverse($heroPicks); ?>
        $winrate = array_reverse($winrate);



        foreach ($heroPicks as $heroName => $pickCount) {

                                $totalVictory = $winrate[$heroName] ?? 0;
                                $total = ($totalVictory * 100) / $pickCount;
                    ............

enter image description here

Expected result : (leona picked 3 times , 1 victory , 33,3% winrate)

enter image description here

Source: Laravel

Leave a Reply