Pawn Posted April 16, 2009 Share Posted April 16, 2009 Hi. Here's how it is: I have a teams table and a matches table in a MySQL database. I need to display the statistics of each team. To avoid looking through every match each time a team profile is viewed, I have counters which I want to update at intervals. My (limited) understanding is that I can do this with a PHP script executed as a cronjob. My question is: what is the most efficient way of looping through the matches and updating the counters in the teams table? Thanks in advance for any help you can offer. Quote Link to comment https://forums.phpfreaks.com/topic/154392-solved-simple-looping/ Share on other sites More sharing options...
ober Posted April 16, 2009 Share Posted April 16, 2009 We're going to need more info. Where are these "counters" stored? What does a "match" consist of? A description of the DB fields would help. Do you have any code that you've started with? Quote Link to comment https://forums.phpfreaks.com/topic/154392-solved-simple-looping/#findComment-811768 Share on other sites More sharing options...
Pawn Posted April 16, 2009 Author Share Posted April 16, 2009 My teams table includes the counters as matches_won and matches_lost. A match is basically just a list of team IDs, in the form team_id_1, team_id_2 and winner. I just drafted this. I expect it's an ocean away from working or even making sense, but it might give a better of impression of what I want to do: <? $won = 0; $lost = 0; $query_teams = mysql_query("SELECT * FROM teams"); while($current_team = mysql_fetch_array($query_teams)) { $query_matches = mysql_query("SELECT * FROM matches WHERE team_id_1='$current_team[team_id]' OR team_id_2='$current_team[team_id]'"); while($current_match = mysql_fetch_array($query_matches)) { if($current_match['winner'] == $current_team['team_id']) { $won++; } else { $lost++; } } mysql_query("UPDATE teams SET games_won='$won', games_lost='$lost' WHERE team_id='$current_team[team_id]'"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154392-solved-simple-looping/#findComment-811788 Share on other sites More sharing options...
gurroa Posted April 16, 2009 Share Posted April 16, 2009 I would do it in different way: First count winnings and loosings: $arteam = array(); $query = mysql_query('select team_id from teams order by team_id'); while($row = mysql_fetch_array($query)) { $arteam[$row['team_id']] = array( 'won' => 0, 'lost' => 0 ); } $query = mysql_query('select winner, team_id_1, team_id_2 from matches'); while($row = mysql_fetch_array($query)) { $tm1id = $row['team_id_1']; $tm2id = $row['team_id_2']; if ($row['winner'] == $tm1id) { $arteam[$tm1id]['won'] += 1; $arteam[$tm2id]['lost'] += 1; } else { $arteam[$tm1id]['lost'] += 1; $arteam[$tm2id]['won'] += 1; } } reset($arteam); while(list($id, $wonlostarray) = each($arteam)) { mysql_query(sprintf(" update teams set games_won = %d, games_lost = %d WHERE team_id = %d ", $id, $wonlostarray['won'], $wonlostarray['lost'] )); } Quote Link to comment https://forums.phpfreaks.com/topic/154392-solved-simple-looping/#findComment-811812 Share on other sites More sharing options...
Psycho Posted April 16, 2009 Share Posted April 16, 2009 No, no ,no. You do NOT need to update the Teams table with the results from the Matches table. The whole point of using a relational database is that you can access records across tables in a single query. So, if you have a page to display a team's profile you access ALL the data dynamically in real time. You need to look at how to JOIN tables in your queries. You could do a JOIN in this instance, but I would just use two subqueries. Run this query and you will get all the profile data for a team as well as their win/loss record in a single query. $team_id = 5; // $query = "SELECT *, (SELECT COUNT(*) FROM test WHERE (team_id_1=$team_id OR team_id_2=$team_id) AND winner=$team_id) as wins, (SELECT COUNT(*) FROM test WHERE (team_id_1=$team_id OR team_id_2=$team_id) AND winner<>$team_id) as losses FROM `teams` WHERE teams.team_id = $team_id"; Quote Link to comment https://forums.phpfreaks.com/topic/154392-solved-simple-looping/#findComment-811819 Share on other sites More sharing options...
Pawn Posted April 16, 2009 Author Share Posted April 16, 2009 No, no ,no. I love you guys . Seriously, thanks. Clearly I need to learn to query properly! Quote Link to comment https://forums.phpfreaks.com/topic/154392-solved-simple-looping/#findComment-811829 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.