refactor: getProfileVillages() takes multiple IDs

#313
This commit is contained in:
Martin Ambrus
2017-11-19 15:57:52 +01:00
parent 699781d130
commit 24789507a7
5 changed files with 77 additions and 13 deletions
+8
View File
@@ -181,6 +181,14 @@ if($_GET['aid'])
</thead> </thead>
<tbody> <tbody>
<?php <?php
// preload villages data
$userIDs = [];
foreach($aliusers as $user) {
$userIDs[] = $user['id'];
}
$database->getProfileVillages($userIDs);
// display the page
foreach($aliusers as $user) foreach($aliusers as $user)
{ {
$rank = $rank + 1; $rank = $rank + 1;
+18 -7
View File
@@ -29,12 +29,22 @@ $result = $admin->search_player($_POST['s']);
</tr> </tr>
<?php <?php
if($result){ if($result){
for ($i = 0; $i <= count($result)-1; $i++) {
$varray = $database->getProfileVillages($result[$i]["id"]); // preload villages data
$totalpop = 0; $userIDs = [];
foreach($varray as $vil) { for ($i = 0; $i <= count($result)-1; $i++) {
$totalpop += $vil['pop']; $userIDs[] = $result[$i]["id"];
} }
$database->getProfileVillages($userIDs);
// display
for ($i = 0; $i <= count($result)-1; $i++) {
$varray = $database->getProfileVillages($result[$i]["id"]);
$totalpop = 0;
foreach($varray as $vil) {
$totalpop += $vil['pop'];
}
echo ' echo '
<tr> <tr>
<td>'.$result[$i]["id"].'</td> <td>'.$result[$i]["id"].'</td>
@@ -43,7 +53,8 @@ echo '
<td>'.$totalpop.'</td> <td>'.$totalpop.'</td>
</tr> </tr>
'; ';
}} }
}
else{ else{
echo ' echo '
<tr> <tr>
+9
View File
@@ -4644,6 +4644,15 @@ class Automation {
global $database; global $database;
$q = "SELECT id, invited FROM ".TB_PREFIX."users WHERE invited > 0"; $q = "SELECT id, invited FROM ".TB_PREFIX."users WHERE invited > 0";
$array = $database->query_return($q); $array = $database->query_return($q);
// preload villages data
$userIDs = [];
foreach($array as $user) {
$userIDs[] = $user['id'];
}
$database->getProfileVillages($userIDs);
// continue...
foreach($array as $user) { foreach($array as $user) {
$numusers = mysqli_fetch_array(mysqli_query($GLOBALS['link'],"SELECT Count(*) as Total FROM ".TB_PREFIX."users WHERE id = ".(int) $user['invited']), MYSQLI_ASSOC); $numusers = mysqli_fetch_array(mysqli_query($GLOBALS['link'],"SELECT Count(*) as Total FROM ".TB_PREFIX."users WHERE id = ".(int) $user['invited']), MYSQLI_ASSOC);
if($numusers['Total'] > 0){ if($numusers['Total'] > 0){
+33 -6
View File
@@ -1561,19 +1561,46 @@ class MYSQLi_DB implements IDbConnection {
} }
function getProfileVillages($uid, $use_cache = true) { function getProfileVillages($uid, $use_cache = true) {
list($uid) = $this->escape_input((int) $uid); $arrayPassed = is_array($uid);
if (!$arrayPassed) {
$uid = [(int) $uid];
} else {
foreach ($uid as $index => $uidValue) {
$uid[$index] = (int) $uidValue;
}
}
if (!count($uid)) {
return [];
}
// first of all, check if we should be using cache and whether the field // first of all, check if we should be using cache and whether the field
// required is already cached // required is already cached
if ($use_cache && ($cachedValue = self::returnCachedContent(self::$profileVillagesCache, $uid)) && !is_null($cachedValue)) { if ($use_cache && !$arrayPassed && ($cachedValue = self::returnCachedContent(self::$profileVillagesCache, $uid[0])) && !is_null($cachedValue)) {
return $cachedValue; return $cachedValue;
} }
$q = "SELECT capital,wref,name,pop,created from " . TB_PREFIX . "vdata where owner = $uid order by pop desc"; $q = "SELECT capital,wref,name,pop,created,owner from " . TB_PREFIX . "vdata where owner IN(".implode(', ', $uid).") order by pop desc";
$result = mysqli_query($this->dblink,$q); $result = mysqli_query($this->dblink,$q);
self::$profileVillagesCache[$uid] = $this->mysqli_fetch_all($result); if (!$arrayPassed) {
return self::$profileVillagesCache[$uid]; $result = $this->mysqli_fetch_all($result);
self::$profileVillagesCache[ $uid[0] ] = $result;
} else {
// we're preloading, cache all the data individually
if (mysqli_num_rows($result)) {
while ( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) ) {
if ( ! isset( self::$profileVillagesCache[ $row['owner'] ] ) ) {
self::$profileVillagesCache[ $row['owner'] ] = [];
}
self::$profileVillagesCache[ $row['owner'] ][] = $row;
}
}
}
return $result;
} }
// no need to refactor this method // no need to refactor this method
+9
View File
@@ -126,6 +126,15 @@ if($aid == $session->alliance){
<?php <?php
// Alliance Member list loop // Alliance Member list loop
$rank=0; $rank=0;
// preload villages data
$userIDs = [];
foreach($memberlist as $member) {
$userIDs[] = $member['id'];
}
$database->getProfileVillages($userIDs);
// continue...
foreach($memberlist as $member) { foreach($memberlist as $member) {
$rank = $rank+1; $rank = $rank+1;