MySQL Forums
Forum List  »  Newbie

Display sum of table in one line of text?
Posted by: Matthew Jones
Date: March 28, 2005 12:30PM

Hi,
I am fairly new to PHP and MySQL, but have successfully managed to get a table on my website, with it successully pulling information from a MySQL table. I even have a form for updating/adding new records to the database, thanks to some of the great tutorials online. However, what I want to do now, is to display the sum of the number in a column, as a number in a text line at the bottom of the table. eg:

Column A
5
4
1
-----------
Column A Total: 9

I have searched high and low on google, and cannot find anything that relates to what I want to appear: Just a line of text, where the sum of the column changes accordingly with the amounts in the database. So far, I have a php query (courtesy of phpMyAdmin):

$sql = 'SELECT SUM(kills) FROM `tfcoop` WHERE 1';

How can I implement this to display the result in a line of text?

Here is the php so far, customised from a php tutorial:

------------------------------------------------------------------------
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>

<?php

$db = mysql_connect("localhost", "tangoforce_com", "aa375706");



mysql_select_db("tangoforce_com_1",$db);



$result = mysql_query("SELECT * FROM tfcoop ORDER BY score DESC",$db);

exec(
"cp $picture http://www.tangoforce.com/images/flags/$nat";);



echo "<table border=1>\n";



echo "<tr><td class=tableheader align=center>Member</td><td class=tableheader align=center>Nationality</td><td

class=tableheader align=center>Kills</td><td class=tableheader align=center>Deaths</td><td class=tableheader

align=center>Score</td></tr>\n";



while ($myrow = mysql_fetch_row($result)) {



printf("<tr><td align=center>%s</td><td align=center><img src=images/flags/%s></td><td align=center>%s</td><td

align=center>%s</td><td align=center>%s</td></tr>\n", $myrow[1], $myrow[2], $myrow[3], $myrow[4], $myrow[5]);







}



echo "</table>\n";


?>
</html>
------------------------------------------------------------------------

It is for my 'clan' website, and absically, I want it to display the total of the 'kills' column, then (on a new line) the total of the 'deaths' column, then I want it to display the result of the 'kill total' divided by the 'death total'.

Hope I haven't rambled too much, and I hope someone can help.
Thanks. :D

Options: ReplyQuote


Subject
Written By
Posted
Display sum of table in one line of text?
March 28, 2005 12:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.