Oh, okay. I got lost somewhere in the middle. I thought your question was originally "how do I do it with two queries", but then I saw you trying to do it with one. When the results of the single query were incorrect, you said "but I can get the correct results with two queries", so I thought you wanted to switch it to one.
I see now (do I?) that your question isn't about the queries at all: it's how to get the results of two different queries organized so that it's easy for PHP to generate one line of code.
Easy: arrays.
$result = mysql_query($first_assist_query);
if ($result !== FALSE)
{
while ($row = mysql_fetch_array($result))
{
$result_array[$row['playerID']]['1st assist'] = $row['first_assist'];
}
}
$result = mysql_query($second_assist_query);
if ($result !== FALSE)
{
while ($row = mysql_fetch_array($result))
{
$result_array[$row['playerID']]['2nd assist'] = $row['second_assist'];
}
}
Now, $result_array will be a two-level array. The first level index is the player ID. The second level indexes are "1st assist" and "2nd assist".
In my toy example above, that would result in an array structured like this:
$result_array = Array
(
[1] => Array
(
[1st assist] => 2
[2nd assist] => 1
)
[2] => Array
(
[1st assist] => 1
[2nd assist] => 1
)
[3] => Array
(
[2nd assist] => 1
)
)
Then, when you're creating each line of HTML, you can use that single combined-results array:
echo "<table>\n";
echo " <tr><th>Player ID</th><th>1st Assists</th><th>2nd Assists</th></tr>\n";
foreach($results_array as $player_id => $assist_array)
{
echo " <tr>\n";
echo " <td>" . (isset($assist_array['1st assist'])? $assist_array['1st assist'] : 0) . "</td>\n";
echo " <td>" . (isset($assist_array['2nd assist'])? $assist_array['2nd assist'] : 0) . "</td>\n";
echo " </tr>\n";
}
echo "</table>\n";
This scheme has only one problem: your query isn't guaranteed to return all the players. That's why we need the isset() when outputting the results -- because a player that has one kind of assist might not have another. Other players might not have any.
You should be able to rewrite your queries to return 0 counts for players that exist in the PH_PLAYER table but do not have any scoring assists.
Did I answer your question finally? =)
Take care,
Nik
http://www.bigaction.org/