Jump to content

Adding figures in database to get totals


ryanwood4

Recommended Posts

I am trying to create a database containing results from races. Each driver has a column in a table, which records how many points they got in each race. The races are ordered as 1,2,3... under the id column.

 

I want to show the results, but am struggling to find a way to make them add up.

 

The code i'm using displays the first results for each driver, but I want to add up all the figures in each column for each driver so it gives a total of their points.

 

<?php
$con = mysql_connect("xxxx","xxxx","xxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("xxxx_championship", $con);

$result = mysql_query("SELECT * FROM wdc ORDER BY id");

while($row = mysql_fetch_array($result))
  {
    $alonso = $row['alonso'];
    $alguersuari = $row['alguersuari'];
    $badoer = $row['badoer'];
    $barrichello = $row['barrichello'];
    $buemi = $row['buemi'];
    $button = $row['button'];
    $fisichella = $row['fisichella'];
    $glock = $row['glock'];
    $grosjean = $row['grosjean'];
    $hamilton = $row['hamilton'];
    $heidfeld = $row['heidfeld'];
    $kovalainen = $row['kovalainen'];
    $kubica = $row['kubica'];
    $massa = $row['massa'];
    $nakajima = $row['nakajima'];
    $piquet = $row['piquet'];
    $raikkonen = $row['raikkonen'];
    $rosberg = $row['rosberg'];
    $sutil = $row['sutil'];
    $trulli = $row['trulli'];
    $vettel = $row['vettel'];
    $webber = $row['webber'];
    $liuzzi = $row['liuzzi'];
    $bourdais = $row['bourdais'];
  }

mysql_close($con);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" >
<head>
<title>The F1 Times</title>
</head>
<body>

J. Button <?php echo $button; ?><br />
R. Barrichello <?php echo $barrichello; ?><br />
M. Webber <?php echo $webber; ?><br />
S. Vettel <?php echo $vettel; ?><br />
K. Raikkonen <?php echo $raikkonen; ?><br />
N. Rosberg <?php echo $rosberg; ?><br />
L. Hamilton <?php echo $hamilton; ?><br />
F. Alonso <?php echo $alonso; ?><br />
F. Massa <?php echo $massa; ?><br />
J. Trulli <?php echo $trulli; ?><br />
T. Glock <?php echo $glock; ?><br />
R. Kubica <?php echo $kubica; ?><br />
N. Heidfeld <?php echo $heidfeld; ?><br />
S. Buemi <?php echo $buemi; ?><br />
K. Nakajima <?php echo $nakajima; ?><br />
H. Kovalainen <?php echo $kovalainen; ?><br />
T. Liuzzi <?php echo $liuzzi; ?><br />
A. Sutil <?php echo $sutil; ?><br />
G. Fisichella <?php echo $fisichella; ?><br />
N. Piquet<?php echo $piquet; ?><br />
J. Alguersuari <?php echo $alguersuari; ?><br />
L. Badoer <?php echo $badoer; ?><br />
S. Bourdais <?php echo $bourdais; ?><br />
R. Grosjean <?php echo $grosjean; ?><br />

</body>
</html>

 

Any help is greatly appreciated, i'm pretty new to PHP. Thanks.

You use SQL's SUM() function

 

You might also consider restructuring your database table so that you have a drivers table, a race table, and a results table rather than a column for each driver in the wdc table. What would you have done if Schumacher had stepped in to replace Felipe Massa? In fact, what did you do?

Hard-coding your column names as actual data values is bad practise, and makes it a lot harder to make any changes (in this case with a driver being replaced)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.