Anyone know?
Thanks
Moderator: General Moderators
Code: Select all
<table width="100%">
<tr>
<th>Name</th>
<th>Games Played</th>
<th>Overs Bowled</th>
<th>Total Runs Scored</th>
<th>Batting Average</th>
<th>Runs Conceded</th>
<!-- <th>Wickets Taken</th>
<th>Bowling Average</th> -->
</tr>
<?php
$playersResult = mysql_query("SELECT name, SUM(`battingscore`) AS `sumScore` FROM battingstats GROUP BY name");
while($playersRow = mysql_fetch_array($playersResult)) {
$playerTotalGamesResult = mysql_query("SELECT COUNT(`name`) AS `totalGames` FROM battingstats GROUP BY name");
$playerTotalGamesRow = mysql_fetch_array($playerTotalGamesResult);
$totalGames = $playerTotalGamesRow['totalGames'];
$battingAverage = $playersRow['sumScore'] / $totalGames;
$oversBowledResult = mysql_query("SELECT COUNT(`name` AS `oversBowled` FROM bowlingstats GROUP BY name");
$oversBowledRow = mysql_fetch_array($oversBowledResult);
$oversBowled = $oversBowledRow['oversBowled'];
$runsConcededResult = mysql_query("SELECT SUM(`runsconceded` AS `totalRuns` FROM bowlingstats GROUP BY name");
$runsConcededRow = mysql_fetch_array($runsConcededRow);
$runsConceded = $runsConcededRow['totalRuns'];
echo "<tr>";
echo "<td align=\"center\">" . $playersRow['name'] . "</td>";
echo "<td align=\"center\">" . $totalGames . "</td>";
echo "<td align=\"center\">" . $oversBowled . "</td>";
echo "<td align=\"center\">" . $playersRow['sumScore'] . "</td>";
echo "<td align=\"center\">" . $battingAverage . "</td>";
echo "<td align=\"center\">" . $runsConceded . "</td>";
echo "</tr>";
} ?>
</table>I fixed all the queries but it still wont display what I want, infact it throws no PHP error but outputs on results no matter what I try...pytrin wrote:There's no reason to run the other queries, as they are, inside the loops. Since you use no filtering criteria that is derived inside the loop, it might as well be run once before the loop starts and values reused inside the loop. Aside from that, some of the queries are missing closing brackets ')' on functions such as SUM and COUNT.
Code: Select all
<table width="100%">
<tr>
<th>Name</th>
<th>Games Played</th>
<th>Overs Bowled</th>
<th>Total Runs Scored</th>
<th>Batting Average</th>
<th>Runs Conceded</th>
<!-- <th>Wickets Taken</th>
<th>Bowling Average</th> -->
</tr>
<?php
$playersResult = mysql_query("SELECT name, SUM(`battingscore`) AS `sumScore` FROM battingstats GROUP BY name");
$runsConcededResult = mysql_query("SELECT SUM(`runsconceded`) AS `totalRuns` FROM bowlingstats GROUP BY name");
$oversBowledResult = mysql_query("SELECT COUNT(`name`) AS `oversBowled` FROM bowlingstats GROUP BY name");
while($oversBowledRow = mysql_fetch_array($oversBowledResult)) {
$oversBowled = $oversBowledRow['oversBowled'];
$oversBowledArray[] = $oversBowled;
}
while($runsConcededRow = mysql_fetch_array($runsConcededResult)) {
$runsConceded = $runsConcededRow['totalRuns'];
$runsConcededArray[] = $runsConceded;
}
$loopVar = 0;
while($playersRow = mysql_fetch_array($playersResult)) {
$playerTotalGamesResult = mysql_query("SELECT COUNT(`name`) AS `totalGames` FROM battingstats GROUP BY name");
$playerTotalGamesRow = mysql_fetch_array($playerTotalGamesResult);
$totalGames = $playerTotalGamesRow['totalGames'];
$battingAverage = $playersRow['sumScore'] / $totalGames;
echo "<tr>";
echo "<td align=\"center\">" . $playersRow['name'] . "</td>";
echo "<td align=\"center\">" . $totalGames . "</td>";
echo "<td align=\"center\">" . $oversBowledArray[$loopVar] . "</td>";
echo "<td align=\"center\">" . $playersRow['sumScore'] . "</td>";
echo "<td align=\"center\">" . $battingAverage . "</td>";
echo "<td align=\"center\">" . $runsConcededArray[$loopVar] . "</td>";
echo "</tr>";
$loopVar++;
} ?>
</table>The tables are not linked in anyway, no, the way this system works having user_id's isn't going to work very well. They don't register and I just think it would complicate things.pytrin wrote:Is there a relationship between the 'battingstats' and 'bowlingstats' tables? it looks like the user name is used by both, but do you have a unique identifier such as 'user_id'? it's best if you posted the structure of your tables here