PDA

View Full Version : Query Results for() while()


Adrian Schneider
04-07-2005, 03:44 AM
Hi, I'm trying to sort through the data to display 1 team at a time (while gathering tons of other information).

I need to get fc_team.managerid, fc_team.id, fc_team.playerid, fc_player_stats.score, fc_playerownership.playerid, fc_playerownership.roleid.

Table relations:
fc_team.id=fc_playerownership.teamid
fc_player_stats.playerid=fc_playerownership.player id

Field Info:
Each player in a given team will either have a roleid of 1-27, so for each team, there should be 27 players/results.

So basically I need to eval('$fantasy_cricket_teamplayerbit .= "' . fetch_template('fantasy_cricket_teamplayerbit') . '";'); for each unique team, while gathering the scores for each player and have them calculated (there is some math involved here, not just addition) and the TOTAL calculated scores of all players added up and displayed per row along with the manager name.

So how should I do this? I tried using for() but that didn't help much, because I don't know how many rows to set the maximum (depends on the # of results from the query) the score selection messes this up, because it selects every player.

I know I have to use while() on the array, and for() afterwards, but I'm pretty stuck, so any help would be appreciated.

WhSox21
04-07-2005, 04:01 AM
I'm not sure what exactly you need here but this is what you would do if I understand you correctly:


while($row = $DB_site->fetch_array($query_results))
{
// extra info here
}

Adrian Schneider
04-07-2005, 05:20 AM
Thanks for reply

No I'm not that imcompetent, well not anymore anyway. :o

I need to only show my fantasy_cricket_teamplayerbit for every unique team, right now if I did while on the array, I would get a hundred or so results, depending on teams/players. Also to retrieve all the 'scores' of each player, then apply some math to them, then the final number I get by summing all the final numbers up, and show that total with each unique team.

WhSox21
04-07-2005, 06:09 PM
Add DISTINCT to your query. That will return every unique team. Other than that you'll need to fix your query to sum up everything.

Link14716
04-07-2005, 09:59 PM
Maybe something like this.
$query = mysql_query("....");
while ($results = mysql_fetch_array($query)) {
// Sort each row into the id.
$array[$results[id]]['rows'][] = $results;
// Other things not specific per row.
$array[$results[id]['blah'] = $result['blah']
}
// Go through each team.
foreach ($array as $id => $team) {
// Go through each row.
foreach ($team['rows'] as $row) {
// Do stuff here, like calculations and stuff.
}
// Finish doing team related things.
eval('$fantasy_cricket_teamplayerbit .= "' . fetch_template('fantasy_cricket_teamplayerbit') . '";');
}

<3 foreach.

Adrian Schneider
04-13-2005, 05:39 AM
Thanks Link, that worked.

Since I had to get this to work on a per match basis (game = parent) I threw a while(matchquery) around it. Then I threw each team into an array to display so I can get it to display what I need (but that works).

Well since I did that, somewhere along the way the part that seperates teams stopped working.

I'll post that section of the code, and the $array values in a link.


while($teaminfo = $DB_site->fetch_array($viewteams))
{
$array[$teaminfo[id]]['blah'] = $teaminfo['blah'];
$array[$teaminfo[id]]['rows'][] = $teaminfo;
}

// Process Each Team
foreach ($array as $id => $team)
{
// Process Each Player
foreach ($team['rows'] as $row)
{
...
}
// calculations, etc
}
I'm pretty sure the blah stuff didn't affect it, anyway here are the 2 arrays.
http://world-a-team.com/aj/fantasy_cricket.php?do=viewgame&id=20&show=yes
So everytime the roleid hits 27 and resets, it should be a new team.

** solved

thanks MarcoH64