To populate a player rollcall, an extensive query pulls data from tables in my external MySQL database. Part of this also extracts the player(s) who a) made the most appearances and b) scored the most goals. For a couple of reasons, I now want to move this into functions.php
and output the most appearances AND most goals as separate shortcodes from one function. Is this possible? I've only managed to do one or the other at the moment.
The shortcodes I'm aiming to create are [seasonapps]
and [seasongoals]
. And the intention is that the first outputs the player who made the total number of appearances, while the second outputs the player who scored the most goals.
The function currently looks like this (appreciate it's a lot – have truncated parts for brevity):
function seasonapps ( $atts ){
global $boroguideDB;
$seasonID = get_the_title();
$appsetup = $boroguideDB->get_results("SELECT re.date, re.venue, rep, re.opponent, re.score, re.att, ce.name, ce.ID, re.agg, re.ID, se.blurb, se.ID, re.season, repkey FROM resultengine re INNER JOIN clubengine ce ON re.opponent=ce.ID INNER JOIN seasonengine se ON se.season=re.season WHERE (re.season='".$seasonID."') ORDER BY re.date ASC", ARRAY_A);
foreach($appsetup as $row) {
$myHashMap = array();
if (!function_exists('processAppsUpdate')) {
function processAppsUpdate($myHashMap, $row) {
$id = $row['game'];
if (array_key_exists($id, $myHashMap)) {
$tempApps = $myHashMap[$id]['apps'];
$myHashMap[$id]['apps'] = $tempApps + $row['count'];
$tempTotal = $myHashMap[$id]['totalapps'];
$myHashMap[$id]['totalapps'] = $tempTotal + $row['count'];
} else {
$tempArray = array();
$tempArray[$id] = $id;
$tempArray['fname'] = $row['first'];
$tempArray['lname'] = $row['last'];
$tempArray['playerID'] = $row['players'];
$tempArray['totalapps'] = $row['count'];
$tempArray['apps'] = $row['count'];
$tempArray['subs'] = 0;
$tempArray['unused'] = 0;
$tempArray['goals'] = 0;
$tempArray['yellow'] = 0;
$tempArray['red'] = 0;
$tempArray['playerid'] = $id;
$myHashMap[$id] = $tempArray;
}
return $myHashMap;
}
function processGoalsUpdate($myHashMap, $row) {
$id = $row['game'];
if (array_key_exists($id, $myHashMap)) {
$tempApps = $myHashMap[$id]['goals'];
$myHashMap[$id]['goals'] = $tempApps + $row['count'];
} else {
$tempArray = array();
$tempArray[$id] = $id;
$tempArray['fname'] = $row['first'];
$tempArray['lname'] = $row['last'];
$tempArray['playerID'] = $row['players'];
$tempArray['apps'] = 0;
$tempArray['unused'] = 0;
$tempArray['goals'] = $row['count'];
$tempArray['yellow'] = 0;
$tempArray['red'] = 0;
$tempArray['subs'] = 0;
$tempArray['playerid'] = $id;
$myHashMap[$id] = $tempArray;
}
return $myHashMap;
}
$PL1 = $boroguideDB->get_results("SELECT mt.ID1 as game,pe.firstname as first,pe.lastname as last,pe.ID as players,COUNT(*) as count FROM matchteam mt RIGHT JOIN playerengine pe ON mt.ID1=pe.ID RIGHT JOIN resultengine re ON mt.ID=re.ID WHERE (mt.ID1!='9998') AND (mt.ID1!='0') AND (mt.ID!='1624') AND (mt.ID!='1638') AND (mt.ID!='225') AND (mt.ID!='248') AND (re.season='".$seasonID."') GROUP BY mt.ID1 ORDER BY COUNT(*) DESC", ARRAY_A);
foreach($PL1 as $row) {
$myHashMap = processAppsUpdate($myHashMap, $row);
}
$PL2 = $boroguideDB->get_results("SELECT mt.ID2 as game,pe.firstname as first,pe.lastname as last,pe.ID as players,COUNT(*) as count FROM matchteam mt RIGHT JOIN playerengine pe ON mt.ID2=pe.ID RIGHT JOIN resultengine re ON mt.ID=re.ID WHERE (mt.ID2!='9998') AND (mt.ID2!='0') AND (mt.ID!='1624') AND (mt.ID!='1638') AND (mt.ID!='225') AND (mt.ID!='248') AND (re.season='".$seasonID."') GROUP BY mt.ID2 ORDER BY COUNT(*) DESC", ARRAY_A);
foreach($PL2 as $row) {
$myHashMap = processAppsUpdate($myHashMap, $row);
}
...
$S1 = $boroguideDB->get_results("SELECT ms.S1 as game,pe.firstname as first,pe.lastname as last,pe.ID as players,COUNT(*) as count FROM matchscorer ms RIGHT JOIN playerengine pe ON ms.S1=pe.ID RIGHT JOIN resultengine re ON ms.ID=re.ID WHERE (ms.S1!='0') AND (ms.ID!='1624') AND (ms.ID!='1638') AND (ms.ID!='225') AND (ms.ID!='248') AND (re.season='".$seasonID."') GROUP BY ms.S1 ORDER BY COUNT(*) DESC", ARRAY_A);
foreach($S1 as $row) {
$myHashMap = processGoalsUpdate($myHashMap, $row);
}
$S2 = $boroguideDB->get_results("SELECT ms.S2 as game,pe.firstname as first,pe.lastname as last,pe.ID as players,COUNT(*) as count FROM matchscorer ms RIGHT JOIN playerengine pe ON ms.S2=pe.ID RIGHT JOIN resultengine re ON ms.ID=re.ID WHERE (ms.S2!='0') AND (ms.ID!='1624') AND (ms.ID!='1638') AND (ms.ID!='225') AND (ms.ID!='248') AND (re.season='".$seasonID."') GROUP BY ms.S2 ORDER BY COUNT(*) DESC", ARRAY_A);
foreach($S2 as $row) {
$myHashMap = processGoalsUpdate($myHashMap, $row);
}
...
$maxTotalApps = 0;
$maxTotalApps = 0;
$maxTotalAppsPlayer = "";
$maxGoals = 0;
$maxGoalsPlayer = "";
foreach (array_keys($myHashMap) as $hashMapKey) {
$totapps = isset($myHashMap[$hashMapKey]['totalapps']) ? $myHashMap[$hashMapKey]['totalapps'] : '';
if ($totapps > $maxTotalApps) {
$maxTotalApps = $myHashMap[$hashMapKey]['totalapps'];
$maxTotalAppsPlayer = $myHashMap[$hashMapKey]['fname']." ".$myHashMap[$hashMapKey]['lname'];
} else if ($totapps == $maxTotalApps) {
$maxTotalAppsPlayer = $maxTotalAppsPlayer." / ".$myHashMap[$hashMapKey]['fname']." ".$myHashMap[$hashMapKey]['lname'];
}
}
}
}
return $maxTotalAppsPlayer." - ".$maxTotalApps;
}
add_shortcode( 'seasonapps', 'seasonapps' );
For the second shortcode, I have got no further than – but see it's a separate function from the other and therefore isn't making the relationship with the query in the first:
function seasongoals ( $atts ){
return $maxGoalsPlayer." - ".$maxGoals;
}
add_shortcode( 'seasongoals', 'seasonapps' );
Can the two be combined? I've referred to other articles such as this and this but not sure the questions being asked are the same as this one.
And, if a second shortcode output can be generated from the original function, can a third or fourth etc.?