Jump to content

[SOLVED] Adding numbers from a query


Canman2005

Recommended Posts

hi all

 

i have the following query

 

            <?php
		$sql1 = "SELECT * FROM bes_optionals ORDER BY title ASC";
		$show1 = @mysql_query($sql1,$connection) or die(mysql_error());
		while ($row1 = mysql_fetch_array($show1))
		{
		if($row1['type'] == 1)
		{
		if($_GET[$row1['id']] == 1)
		{
		print $row1['cost'];
		}
		}
		elseif($row1['type'] == 2)
		{
		if($_GET[$row1['id']] == 1)
		{
		print $row1['cost'];
		}
		elseif($_GET[$row1['id']] == 2)
		{
		print $row1['cost']*2;
		}
		elseif($_GET[$row1['id']] == 3)
		{
		print $row1['cost']*3;
		}
		elseif($_GET[$row1['id']] == 4)
		{
		print $row1['cost']*4;
		}
		}
		}
            ?>

 

it basically outputs a series of numbers that were selected from a form on the previous page.

 

the output looks something like

 

7.0010

 

which is

 

7.00

 

and

 

10

 

is it possible to add up all numbers outputted so it gives a result of 17?

 

any help would be ace

 

thanks in advance

 

ed

Link to comment
https://forums.phpfreaks.com/topic/78859-solved-adding-numbers-from-a-query/
Share on other sites

Give this a try

 

<?php

$sql1 = "SELECT * FROM bes_optionals ORDER BY title ASC";
$show1 = @mysql_query($sql1,$connection) or die(mysql_error());
$cost = 0;

while ($row1 = mysql_fetch_array($show1)) {

    if ($row1['type'] == 1) {
    
        if ($_GET[$row1['id']] == 1) {
            $cost += $row1['cost'];
        }
        
    } else if ($row1['type'] == 2) {
    
        if ($_GET[$row1['id']] == 1) {
            $cost += $row1['cost'];
        } else if ($_GET[$row1['id']] == 2) {
            $cost += $row1['cost']*2;
        } else if ($_GET[$row1['id']] == 3) {
            $cost += $row1['cost']*3;
        } else if ($_GET[$row1['id']] == 4) {
            $cost += $row1['cost']*4;
        }
    }
}

echo "Total: " . $cost;

?>

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.