Front Office Football Central  

Go Back   Front Office Football Central > Main Forums > Off Topic
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 08-24-2010, 05:10 PM   #1
Ben E Lou
Morgado's Favorite Forum Fascist
 
Join Date: Oct 2000
Location: Greensboro, NC
PING: php people

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.

Code:
$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.

Code:
array_multisort($pct,SORT_DESC); echo ""; echo ""; foreach ($pct as $playerid=>$temp1) {foreach ($temp1 as $yr=>$percent) {echo ""; echo "";}} echo "
playeridyearpct
{$playerid}{$yr}{$percent}
";
I can't get it to display playerid

Two issues.

1. I want to sort on percentage. It's not doing it.
2.It's not returning the player id at all. RESOLVED.

Here are the results of the code.

playeridyearpct

201140.3

202029.6

201830.2

201249.5

201428.9

201327.6

202633.9

201042.2

201738.5

200942.6

202439.1

200452.4

200555.3

200746.1

202135.2

202231.3

202342.8

201531.1

202543.3

200641.9

201932.3



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.)
__________________
The media don't understand the kinds of problems and pressures 54 million come wit'!


Last edited by Ben E Lou : 08-24-2010 at 06:28 PM.
Ben E Lou is offline   Reply With Quote
Old 08-24-2010, 05:11 PM   #2
Ben E Lou
Morgado's Favorite Forum Fascist
 
Join Date: Oct 2000
Location: Greensboro, NC
Heh. It's removing some of my table tags from the code, but I'm sure the problem is in the foreach statements and the array sort function.
__________________
The media don't understand the kinds of problems and pressures 54 million come wit'!
Ben E Lou is offline   Reply With Quote
Old 08-24-2010, 05:17 PM   #3
cartman
Death Herald
 
Join Date: Nov 2000
Location: Le stelle la notte sono grandi e luminose nel cuore profondo del Texas
Don't you have to use 'usort' in there somewhere?
__________________
Thinkin' of a master plan
'Cuz ain't nuthin' but sweat inside my hand
So I dig into my pocket, all my money is spent
So I dig deeper but still comin' up with lint
cartman is offline   Reply With Quote
Old 08-24-2010, 05:21 PM   #4
cartman
Death Herald
 
Join Date: Nov 2000
Location: Le stelle la notte sono grandi e luminose nel cuore profondo del Texas
or, try moving the array_multisort after the foreach commands

edit: yeah, I think this is it, because array_multisort needs an array of columns, not rows.
__________________
Thinkin' of a master plan
'Cuz ain't nuthin' but sweat inside my hand
So I dig into my pocket, all my money is spent
So I dig deeper but still comin' up with lint

Last edited by cartman : 08-24-2010 at 05:32 PM.
cartman is offline   Reply With Quote
Old 08-24-2010, 05:39 PM   #5
Ben E Lou
Morgado's Favorite Forum Fascist
 
Join Date: Oct 2000
Location: Greensboro, NC
That's not it. If I move the statement, it only gives me one row. If I completely remove that statement, I get the same output. I've got something wrong in the syntax that's causing it to do...nothing.
__________________
The media don't understand the kinds of problems and pressures 54 million come wit'!
Ben E Lou is offline   Reply With Quote
Old 08-24-2010, 05:43 PM   #6
Ben E Lou
Morgado's Favorite Forum Fascist
 
Join Date: Oct 2000
Location: Greensboro, NC
Ha. I figured out why playerid isn't showing up...

Code:
$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";

Hey, dummy! Player ID isn't gonna show up IF YOU DON'T HAVE IT IN THE QUERY!!!
__________________
The media don't understand the kinds of problems and pressures 54 million come wit'!

Last edited by Ben E Lou : 08-24-2010 at 05:43 PM.
Ben E Lou is offline   Reply With Quote
Old 08-24-2010, 05:43 PM   #7
cartman
Death Herald
 
Join Date: Nov 2000
Location: Le stelle la notte sono grandi e luminose nel cuore profondo del Texas
Here's a sample snippet I found that pointed me towards moving the array_multisort:

Code:
// Obtain a list of columns foreach ($data as $key => $row) { $volume[$key] = $row['volume']; $edition[$key] = $row['edition']; } // Sort the data with volume descending, edition ascending // Add $data as the last parameter, to sort by the common key array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);
__________________
Thinkin' of a master plan
'Cuz ain't nuthin' but sweat inside my hand
So I dig into my pocket, all my money is spent
So I dig deeper but still comin' up with lint
cartman is offline   Reply With Quote
Old 08-24-2010, 06:06 PM   #8
Ben E Lou
Morgado's Favorite Forum Fascist
 
Join Date: Oct 2000
Location: Greensboro, NC
I don't think that's it. Moving it around isn't helping. I'm not terribly good with arrays yet, so I'm not certain, though.
__________________
The media don't understand the kinds of problems and pressures 54 million come wit'!
Ben E Lou is offline   Reply With Quote
Old 08-25-2010, 08:25 AM   #9
lordscarlet
Pro Starter
 
Join Date: Oct 2005
Location: Washington, DC
If I find time today I'll try to play with this. But I don't use PHP enough to know the answer without trying it.
__________________
Sixteen Colors ANSI/ASCII Art Archive

"...the better half of the Moores..." -cthomer5000
lordscarlet is offline   Reply With Quote
Old 08-25-2010, 08:58 AM   #10
Coffee Warlord
Head Coach
 
Join Date: Oct 2002
Location: Colorado Springs
Edit: Hang on, trying something.

Last edited by Coffee Warlord : 08-25-2010 at 09:01 AM.
Coffee Warlord is offline   Reply With Quote
Old 08-25-2010, 09:18 AM   #11
fantom1979
College Benchwarmer
 
Join Date: Apr 2008
Location: Sterling Heights, Mi
Sorry, I can't be much help. I avoid arrays and will go to great lengths to code without them. If you figure it out, please post it. I run into problems like this all the time. If I see the solution, it might help my own multi-dimensional array problems
fantom1979 is offline   Reply With Quote
Old 08-25-2010, 09:35 AM   #12
lordscarlet
Pro Starter
 
Join Date: Oct 2005
Location: Washington, DC
Quote:
Originally Posted by fantom1979 View Post
Sorry, I can't be much help. I avoid arrays and will go to great lengths to code without them. If you figure it out, please post it. I run into problems like this all the time. If I see the solution, it might help my own multi-dimensional array problems

I have no idea how you can code while avoiding arrays!
__________________
Sixteen Colors ANSI/ASCII Art Archive

"...the better half of the Moores..." -cthomer5000
lordscarlet is offline   Reply With Quote
Old 08-25-2010, 09:51 AM   #13
fantom1979
College Benchwarmer
 
Join Date: Apr 2008
Location: Sterling Heights, Mi
Quote:
Originally Posted by lordscarlet View Post
I have no idea how you can code while avoiding arrays!

Since I do not use arrays, I have no idea how you can code while using arrays.

Seriously, I have a fantasy football website for my keeper league. I keep track of standings, stats, records, etc. I have spent a considerable amount of time to set up the databases and record input pages to work without arrays. It would have probably taken me a lot less time to just sit down one day and figure it out.

Last edited by fantom1979 : 08-25-2010 at 09:52 AM.
fantom1979 is offline   Reply With Quote
Old 08-25-2010, 10:02 AM   #14
Coffee Warlord
Head Coach
 
Join Date: Oct 2002
Location: Colorado Springs
I believe this'll do it.

Code:
function checkVal( $a, $b ) { $c1 = $a{percent}; $c2 = $b{percent}; if ( $c1 == $c2 ) return 0; return ( $c1 > $c2 ) ? -1 : 1; } //This is just some test data $array[5][2010] = 7.5; $array[5][2011] = 42.5; $array[8][2010] = 15.5; $array[8][2011] = 12.5; $array[2][2010] = 24.5; $array[2][2011] = 38.5; //End $x = 0; foreach ( $array as $playerid => $temp ) { foreach ( $temp as $year => $percent ) { $new_array[$x]{year} = $year; $new_array[$x]{id} = $playerid; $new_array[$x]{percent} = $percent; $x++; } } usort( $new_array, "checkVal" ); foreach ( $new_array as $row => $final ) { echo "$final[id] - $final[year] ($final[percent])\n"; }

Last edited by Coffee Warlord : 08-25-2010 at 10:04 AM.
Coffee Warlord is offline   Reply With Quote
Old 08-26-2010, 04:25 AM   #15
Ben E Lou
Morgado's Favorite Forum Fascist
 
Join Date: Oct 2000
Location: Greensboro, NC
Thanks for that, CW. I'll give it a try.
__________________
The media don't understand the kinds of problems and pressures 54 million come wit'!
Ben E Lou is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 03:20 PM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.