sum of array rows

Hi all, can any of you wizards help me with a question. I am trying to make a pub quz scoreboard and i am a bit stuck totalling up the round scores. I have a table that stores ‘teamid’, ‘teamname’, and then their round scores ‘round01’, ‘round02’…
I can pull all of this data out of the database and format as an html table but i’m stuck on getting their total score. I want it to be automatically generated from their round scores and dont want to have to input it manually.

Here is my code so far

[php] $query = “SELECT * from scoreboard ORDER BY teamid DESC”;
$data = mysqli_query($dbc, $query);

echo ‘

’;
echo ’'; while ($row = mysqli_fetch_array($data)) echo '
	<td>' . $row['teamname'] . '</td>
	<td>' . $row['round01'] . '</td>
	<td>' . $row['round02'] . '</td>
	<td>' . $row['round03'] . '</td>
	<td>' . $row['round01'+'round02'+'round03'] . '</td>
  </tr>';

echo ‘

Team Name 1 2 3 Total
’;
[/php]

i know
[php]

’ . $row[‘round01’+‘round02’+‘round03’] . '[/php]

is wrong but hopefully you get what i mean.

any help would be greatly appreciated.

Darren

Head on over to php.net and have a look at their array functions, and I believe you can do it right from the query, not sure with mysqli though, as I don’t ever use it, might be the same though (SUM(column') GROUP BYsame column`))

Like this:

[php]

’ . $row[‘round01’] + $row[‘round02’]+ $row[‘round03’] . '[/php]

Simple :
you can edit your mysql query as
[php]$query = “SELECT *, (round01+round02+…) as score from scoreboard ORDER BY teamid DESC”;[/php]

and display using:
[php]$row[‘score’][/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service