Ben E Lou
08-24-2010, 05:10 PM
So I'm running a couple of queries to determine the percentage of his team's total offense that a player was responsible for. Here's that portion.
$query="SELECT year, abbrev, team, rushingyards, receivingyards, sum(rushingyards)+sum(receivingyards) AS yardsfromscrimmage, lastname, firstname
FROM statsbyseason s
Join fof_playerhistorical h on h.id=s.playerid
join fof_teams t on t.id=s.team
group by playerid, year
order by yardsfromscrimmage desc limit 50";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{$query2="SELECT sum(rushingyards)+sum(passyards)-sum(sackedyards) as teamtotalyards
FROM fof_playergamestats s
WHERE year={$row[year]} and team={$row[team]}";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2))
{$pct[$row[playerid]][$row[year]]=number_format(100*$row[yardsfromscrimmage]/$row2[teamtotalyards],1);
}
$team[$row[playerid]]=$row[team];
$abbrev[$row[playerid]]=$row[abbrev];
}
So I've got $pct[playerid][year] as an array. The problem is coming in sorting and displaying it. Here's what I have, and it's wrong.
array_multisort($pct,SORT_DESC);
echo "";
echo "";
foreach ($pct as $playerid=>$temp1)
{foreach ($temp1 as $yr=>$percent)
{echo "";
echo "";}}
echo "<table><tbody><tr><th>playerid</th><th>year</th><th>pct</th></tr><tr><td>{$playerid}</td><td>{$yr}</td><td>{$percent}</td></tr></tbody></table>";I can't get it to display playerid
Two issues.
1. I want to sort on percentage. It's not doing it.
2.<s>It's not returning the player id at all.</s> RESOLVED.
Here are the results of the code.
<table> <tbody><tr><th>playerid</th><th>year</th><th>pct</th></tr><tr><td>
</td><td>2011</td><td>40.3</td></tr><tr><td>
</td><td>2020</td><td>29.6</td></tr><tr><td>
</td><td>2018</td><td>30.2</td></tr><tr><td>
</td><td>2012</td><td>49.5</td></tr><tr><td>
</td><td>2014</td><td>28.9</td></tr><tr><td>
</td><td>2013</td><td>27.6</td></tr><tr><td>
</td><td>2026</td><td>33.9</td></tr><tr><td>
</td><td>2010</td><td>42.2</td></tr><tr><td>
</td><td>2017</td><td>38.5</td></tr><tr><td>
</td><td>2009</td><td>42.6</td></tr><tr><td>
</td><td>2024</td><td>39.1</td></tr><tr><td>
</td><td>2004</td><td>52.4</td></tr><tr><td>
</td><td>2005</td><td>55.3</td></tr><tr><td>
</td><td>2007</td><td>46.1</td></tr><tr><td>
</td><td>2021</td><td>35.2</td></tr><tr><td>
</td><td>2022</td><td>31.3</td></tr><tr><td>
</td><td>2023</td><td>42.8</td></tr><tr><td>
</td><td>2015</td><td>31.1</td></tr><tr><td>
</td><td>2025</td><td>43.3</td></tr><tr><td>
</td><td>2006</td><td>41.9</td></tr><tr><td>
</td><td>2019</td><td>32.3</td></tr></tbody> </table>
What am I doing wrong? (And no lectures about doing this in one query. I'm thinking two is going to be just easier, plus I'd really like to improve my multi-dimensional array skills, and this will help.)
$query="SELECT year, abbrev, team, rushingyards, receivingyards, sum(rushingyards)+sum(receivingyards) AS yardsfromscrimmage, lastname, firstname
FROM statsbyseason s
Join fof_playerhistorical h on h.id=s.playerid
join fof_teams t on t.id=s.team
group by playerid, year
order by yardsfromscrimmage desc limit 50";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{$query2="SELECT sum(rushingyards)+sum(passyards)-sum(sackedyards) as teamtotalyards
FROM fof_playergamestats s
WHERE year={$row[year]} and team={$row[team]}";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2))
{$pct[$row[playerid]][$row[year]]=number_format(100*$row[yardsfromscrimmage]/$row2[teamtotalyards],1);
}
$team[$row[playerid]]=$row[team];
$abbrev[$row[playerid]]=$row[abbrev];
}
So I've got $pct[playerid][year] as an array. The problem is coming in sorting and displaying it. Here's what I have, and it's wrong.
array_multisort($pct,SORT_DESC);
echo "";
echo "";
foreach ($pct as $playerid=>$temp1)
{foreach ($temp1 as $yr=>$percent)
{echo "";
echo "";}}
echo "<table><tbody><tr><th>playerid</th><th>year</th><th>pct</th></tr><tr><td>{$playerid}</td><td>{$yr}</td><td>{$percent}</td></tr></tbody></table>";I can't get it to display playerid
Two issues.
1. I want to sort on percentage. It's not doing it.
2.<s>It's not returning the player id at all.</s> RESOLVED.
Here are the results of the code.
<table> <tbody><tr><th>playerid</th><th>year</th><th>pct</th></tr><tr><td>
</td><td>2011</td><td>40.3</td></tr><tr><td>
</td><td>2020</td><td>29.6</td></tr><tr><td>
</td><td>2018</td><td>30.2</td></tr><tr><td>
</td><td>2012</td><td>49.5</td></tr><tr><td>
</td><td>2014</td><td>28.9</td></tr><tr><td>
</td><td>2013</td><td>27.6</td></tr><tr><td>
</td><td>2026</td><td>33.9</td></tr><tr><td>
</td><td>2010</td><td>42.2</td></tr><tr><td>
</td><td>2017</td><td>38.5</td></tr><tr><td>
</td><td>2009</td><td>42.6</td></tr><tr><td>
</td><td>2024</td><td>39.1</td></tr><tr><td>
</td><td>2004</td><td>52.4</td></tr><tr><td>
</td><td>2005</td><td>55.3</td></tr><tr><td>
</td><td>2007</td><td>46.1</td></tr><tr><td>
</td><td>2021</td><td>35.2</td></tr><tr><td>
</td><td>2022</td><td>31.3</td></tr><tr><td>
</td><td>2023</td><td>42.8</td></tr><tr><td>
</td><td>2015</td><td>31.1</td></tr><tr><td>
</td><td>2025</td><td>43.3</td></tr><tr><td>
</td><td>2006</td><td>41.9</td></tr><tr><td>
</td><td>2019</td><td>32.3</td></tr></tbody> </table>
What am I doing wrong? (And no lectures about doing this in one query. I'm thinking two is going to be just easier, plus I'd really like to improve my multi-dimensional array skills, and this will help.)