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
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;

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.