I have one major module left to accomplish to finish my basic website.
I have a table of taverns, players at those taverns and of points for each player. I need to total the points for each player then print out a list of all the players at one tavern with their point totals and arrange them by their total points.
I decided to find the tavern first. Accomplished.
Then find a list of the players of that tavern. Accomplished.
Then total the points for that player and store it to a variable. Stuck.
I am attempting to step through the list of players with a while statement,
adding up the point totals for a single player. Later I will need to output a list of players and their points, arranged by point totals.
I am experimenting with two possible methods of totaling the points. I don't want anyone to write my code for me. I would just like to know which approach (if either) is likely to be the best.
while($points = ($player_points))
{
array_sum ($player_points);
echo $player_points;
//$total = 0;
//foreach ($points as $type => $player_points)
//{
// $total = $total + $points[$type]['points'];
//}
}
The first obstacle I ran into was an error returned by the players who had no points. The query is expecting an array and there is no array for players with no points. What value can I return which will be an array but have 0 for a points value? I guess I could build an array with the points equal to 0 but there might be an easier way.
This is my search function:
function get_points_by_id($player_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM points ";
$query .= "WHERE player_id=" . $player_id ." ";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// REMEMBER:
// if no rows are returned, fetch_array will return false
if ($player_points = mysql_fetch_array($result_set)) {
return $player_points;
} else {
return NULL;
}
}
In summary 2 questions. Which approach and is there a simple way to return an empty array which will satisfy my while statement.
So far I am trying to build an array:
} else {
"$player_points" => array
("id" => "0",
"player_id" => "1",
"date" => "01/01/2001",
"tnumber" =>"1",
"finished" =>"0",
"points" >="0");
return $player_points;
}
It errors on the first line but I THINK this could be an answer to the bottom part.