Canman2005 Posted November 26, 2007 Share Posted November 26, 2007 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 More sharing options...
pocobueno1388 Posted November 26, 2007 Share Posted November 26, 2007 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 https://forums.phpfreaks.com/topic/78859-solved-adding-numbers-from-a-query/#findComment-399108 Share on other sites More sharing options...
Canman2005 Posted November 26, 2007 Author Share Posted November 26, 2007 nice one thanks very much Link to comment https://forums.phpfreaks.com/topic/78859-solved-adding-numbers-from-a-query/#findComment-399110 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.