DeeTee Skrevet 21. august 2012 Del Skrevet 21. august 2012 Prøver å lage noen leaderboards for en localhive DayZ server, og spørringen fungerte egentlig fint som det var, men bestemte oss for å få til caching av query resultatet, så det ikke skulle bli så mye stress på spillserveren. Har lagt ved ønsker om oppførsel og hele koden i bunn. Spørring SELECT player_data.PlayerName, ROUND(SUM(character_data.HeadshotsZ)/count(*)+(SUM(character_data.DistanceFoot)/15000)/count(*)+(SUM(character_data.KillsB)*40)/count(*)+(SUM(character_data.KillsH)*15)/count(*)+(SUM(character_data.Duration)/count(*)/60)) AS SCORE FROM character_data, player_data where player_data.PlayerID = character_data.PlayerID GROUP BY PlayerName ORDER BY `SCORE` DESC LIMIT 0 , 25 Spørring i phpmyadmin spytter ut: Tansien 259 xell 177 slimjim 105 DaveO 98 tabbi 96 giqu 86 Array - $records Array ( [0] => Array ( [0] => Tansien [PlayerName] => Tansien [1] => 259 [sCORE] => 259 ) [1] => Array ( [0] => xell [PlayerName] => xell [1] => 110 [sCORE] => 110 ) [2] => Array ( [0] => slimjim [PlayerName] => slimjim [1] => 105 [sCORE] => 105 ) [3] => Array ( [0] => DaveO [PlayerName] => DaveO [1] => 98 [sCORE] => 98 ) [4] => Array ( [0] => tabbi [PlayerName] => tabbi [1] => 96 [sCORE] => 96 ) [5] => Array ( [0] => giqu [PlayerName] => giqu [1] => 87 [sCORE] => 87 ) [6] => Array ( [0] => [Metro] Bossen [PlayerName] => [Metro] Bossen [1] => 78 [sCORE] => 78 ) [7] => Array ( [0] => [axe] sarr [PlayerName] => [axe] sarr [1] => 70 [sCORE] => 70 ) [8] => Array ( [0] => [Metro] BadgerVenom [PlayerName] => [Metro] BadgerVenom [1] => 65 [sCORE] => 65 ) [9] => Array ( [0] => Bob Jylland [PlayerName] => Bob Jylland [1] => 61 [sCORE] => 61 ) [10] => Array ( [0] => ghadlan [PlayerName] => ghadlan [1] => 60 [sCORE] => 60 ) [11] => Array ( [0] => [sMD]T0xicGh0st [PlayerName] => [sMD]T0xicGh0st [1] => 59 [sCORE] => 59 ) [12] => Array ( [0] => Zoril [PlayerName] => Zoril [1] => 52 [sCORE] => 52 ) [13] => Array ( [0] => epoxy [PlayerName] => epoxy [1] => 49 [sCORE] => 49 ) [14] => Array ( [0] => [W.K.A] Dildo Faggins [PlayerName] => [W.K.A] Dildo Faggins [1] => 46 [sCORE] => 46 ) [15] => Array ( [0] => Carmack [PlayerName] => Carmack [1] => 46 [sCORE] => 46 ) [16] => Array ( [0] => [LRA]Mombasa [PlayerName] => [LRA]Mombasa [1] => 44 [sCORE] => 44 ) [17] => Array ( [0] => Fowles [PlayerName] => Fowles [1] => 44 [sCORE] => 44 ) [18] => Array ( [0] => Grimfury (AIM) [PlayerName] => Grimfury (AIM) [1] => 44 [sCORE] => 44 ) [19] => Array ( [0] => Fliiix [PlayerName] => Fliiix [1] => 43 [sCORE] => 43 ) [20] => Array ( [0] => [Metro] misC [PlayerName] => [Metro] misC [1] => 42 [sCORE] => 42 ) [21] => Array ( [0] => [KONY] MUGABE [PlayerName] => [KONY] MUGABE [1] => 40 [sCORE] => 40 ) [22] => Array ( [0] => Rasmus [PlayerName] => Rasmus [1] => 40 [sCORE] => 40 ) [23] => Array ( [0] => [N.W.A] Papa T-Large [PlayerName] => [N.W.A] Papa T-Large [1] => 39 [sCORE] => 39 ) [24] => Array ( [0] => [3D] Otto [PlayerName] => [3D] Otto [1] => 39 [sCORE] => 39 ) ) Foreach løkke: foreach ($records as $id=>$row) { echo "<tr>\n"; foreach($row as $col){ echo "<td>"; echo $col; echo "</td>\n"; } echo "</tr>\n"; } Spytter ut: Tansien Tansien 259 259 xell xell 177 177 slimjim slimjim 105 105 DaveO DaveO 98 98 tabbi tabbi 96 96 giqu giqu 86 86 Vil at den skal spytte ut: Tansien 259 xell 177 slimjim 105 DaveO 98 tabbi 96 giqu 86 Tror jeg må combine noe før den siste foreachen, og splitte de opp til slutt - men får det ikke helt til. Hele koden, prøver meg på caching av data. <?php include 'conf'; $file = 'total.txt'; $expire = 3600; // 1 hour if (file_exists($file) && filemtime($file) > (time() - $expire)) { $records = unserialize(file_get_contents($file)); } else { /* form SQL query */ $query = "SELECT player_data.PlayerName, ROUND(SUM(character_data.HeadshotsZ)/count(*)+(SUM(character_data.DistanceFoot)/15000)/count(*)+(SUM(character_data.KillsB)*40)/count(*)+(SUM(character_data.KillsH)*15)/count(*)+(SUM(character_data.Duration)/count(*)/60)) AS SCORE FROM character_data, player_data where player_data.PlayerID = character_data.PlayerID GROUP BY PlayerName ORDER BY `SCORE` DESC LIMIT 0 , 25"; $result = mysql_query($query) or die (mysql_error()); while ($record = mysql_fetch_array($result) ) { $records[] = $record; } $OUTPUT = serialize($records); $fp = fopen($file,"w"); fputs($fp, $OUTPUT); fclose($fp); } // end else // Query results are in $records array function array_unique_deep($array) { $values=array(); //ideally there would be some is_array() testing for $array here... foreach ($array as $part) { if (is_array($part)) $values=array_merge($values,array_unique_deep($part)); else $values[]=$part; } return array_unique($values); } print_r(array_unique_deep($records)); echo "<br/>";echo "<br/>"; print_r($records); echo "<br/>";echo "<br/>"; foreach ($records as $id=>$row) { echo "<tr>\n"; foreach($row as $col){ echo "<td>"; echo $col; echo "</td>\n"; } echo "</tr>\n"; } ?> Lenke til kommentar
DeeTee Skrevet 21. august 2012 Forfatter Del Skrevet 21. august 2012 Løste det, flaut foreach ($records as $id=>$row) { echo "<tr>\n"; echo "<td>"; print $row['Name']; echo "</td>\n"; echo "<td>"; print $row['SCORE']; echo "</td>\n"; echo "</tr>\n"; } Lenke til kommentar
molty Skrevet 22. august 2012 Del Skrevet 22. august 2012 Du kan bruke forrige løsning ved å bruke mysql_fetch_assoc i stedenfor mysql_fetch_array. Fordi mysql_fetch_assoc henter ut alle verdiene i en array med navnet på feltet i databasen, mens mysql_fetch_array henter ut med å lagre det dobbelt opp, først med en index key, og så felt navnet. F.eks: assoc: ['id'] = '1', ['name'] = 'Molty' Array: [0] = '1', ['id'] = '1', [1] = 'Molty', ['name'] ='Molty' Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå