# Subtract calculate problem

Hi
im having a problem subtracting a sum from a total,

i have written a piece of code that adds a % of a whole when a button is selected, what i need to do is to have the option of un selecting the button and having the % taken off.
here is the code i have for adding the % and total

``````\$product = \$total * (1 - \$profit);
\$product1 = \$gametotal * (1 - 0.20);
\$product2 = \$gametotal * (1 - 0.30);
\$product3 = \$gametotal * (1 - 0.50);
``````

how do i subtract the % and total.

thank you paul

Unless (1 - \$profit) is a negative value, it’s not possible for an increase to occur.

Perhaps you chould take us further through what is occurring.

In that case, an effective solution is to store the cancel values for the button, so that you can easily revert back to them again.

Or, you could run through all of the calculations again, taking in to account the current state of the game board.

Some simple math should be able to be used to work that out.

\$gametotal * (1 - \$profit) = \$product

What is \$gametotal equal to?
Divide both sides by 1 - \$profit (assuming that it’s not 0) to solve.

\$gametotal * (1 - \$profit) / (1 - \$profit) = \$product / (1 - \$profit)

which cancels out on the left side to become:

\$gametotal = \$product / (1 - \$profit)

Hi
I will try to expain what it is im trying to do.

i have a bingo game with 15 cards. when a user clicks on a card it sends the values to the database, example ticket cost could be 10p it has to take 10p off there balance in there account, then 20% goes towards 1 line 30% goes onto 2 lines and 50%. fullhouse, then take of a % off wich the admin has set for his profit that could be anything from 1% to 99% depending on how greedy they are. then output the correct values.

here is the code that does all the above. what i want is if they click the button again to cancel the card it reverses all of the above

``````\$sql1 = "INSERT INTO cardA SET id='\$id',username ='\$username', Aone='\$num1',Atwo='\$num2',Athree='\$num3',Afour='\$num4',Afive='\$num5',Asix='\$num6',Aseven='\$num7',Aeight='\$num8',Anine='\$num9',Aten='\$num10',Aeleven='\$num11',Atwelve='\$num12',Athirteen='\$num13',Aforteen='\$num14',Afifteen='\$num15',ticket= 1 ";

if (@mysql_query(\$sql1)) {

} else {
echo '<p>Error adding number: ' .
mysql_error() . '</p>';
}
\$card= \$_POST['card'];

while(\$row = mysql_fetch_array(\$sql)){

}
if (@mysql_query(\$sql1)) {

} else {
echo '<p>Error adding ticket: ' .
mysql_error() . '</p>';
}

\$query1 = sprintf("SELECT ticket FROM rooms4  ",
mysql_real_escape_string(\$senderpay));
\$result = mysql_query(\$query1);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!\$result) {
\$message  = 'Invalid query: ' . mysql_error() . "\
";
\$message .= 'Whole query: ' . \$query1;
die(\$message);
}
while (\$row = mysql_fetch_assoc(\$result)) {
\$ticket= \$row['ticket'];
}
// Perform Query
\$result = mysql_query(\$query1);
\$returnVars = array();
\$returnVars['ticket'] = "\$ticket";
\$returnString = http_build_query(\$returnVars);

//send variables back to Flash
echo "&ticket=".urlencode(\$ticket);

mysql_query("UPDATE investment SET senderpay=(senderpay - '\$ticket')  ");
if (@mysql_query(\$sql1)) {

} else {
echo '<p>Error adding investment: ' .
mysql_error() . '</p>';
}

//update total game money////////////
mysql_query("UPDATE rooms4 SET total=(total + '\$ticket') ");
if (@mysql_query(\$sql1)) {

} else {
echo '<p>Error adding total: ' .
mysql_error() . '</p>';
}
\$query11 = sprintf("SELECT * FROM rooms4  ",
mysql_real_escape_string(\$total));
\$result = mysql_query(\$query11);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!\$result) {
\$message  = 'Invalid query: ' . mysql_error() . "\
";
\$message .= 'Whole query: ' . \$query11;
die(\$message);
}
while (\$row = mysql_fetch_assoc(\$result)) {
\$total= \$row['total'];
\$profit= \$row['profit'];
\$gametotal= \$row['gametotal'];

}
// Perform Query
\$result = mysql_query(\$query11);
\$returnVars = array();
\$returnVars['total'] = "\$total";
\$returnVars['profit'] = "\$profit";
\$returnVars['gametotal'] = "\$gametotal";
\$product = \$total * (1 - \$profit);
\$product1 = \$gametotal * (1 - 0.20);
\$product2 = \$gametotal * (1 - 0.30);
\$product3 = \$gametotal * (1 - 0.50);

\$returnString = http_build_query(\$returnVars);

mysql_query("UPDATE rooms4 SET gametotal='\$product' ");
mysql_query("UPDATE rooms4 SET line1='\$gametotal'-'\$product1' ");
mysql_query("UPDATE rooms4 SET line2='\$gametotal'-'\$product2' ");
mysql_query("UPDATE rooms4 SET fullhouse='\$gametotal'-'\$product3' ");

if (@mysql_query(\$sql1)) {

} else {
echo '<p>Error adding %: ' .
mysql_error() . '</p>';
}
mysql_query("UPDATE rooms4 SET myprofit=('\$myprofit' +'\$total' - gametotal) ");

if (@mysql_query(\$sql1)) {

} else {
echo '<p>Error adding number: ' .
mysql_error() . '</p>';
}

``````

the main bit of code for this

``````\$returnVars = array();
\$returnVars['total'] = "\$total";
\$returnVars['profit'] = "\$profit";
\$returnVars['gametotal'] = "\$gametotal";
\$product = \$total * (1 - \$profit);
\$product1 = \$gametotal * (1 - 0.20);
\$product2 = \$gametotal * (1 - 0.30);
\$product3 = \$gametotal * (1 - 0.50);

\$returnString = http_build_query(\$returnVars);

mysql_query("UPDATE rooms4 SET gametotal='\$product' ");
mysql_query("UPDATE rooms4 SET line1='\$gametotal'-'\$product1' ");
mysql_query("UPDATE rooms4 SET line2='\$gametotal'-'\$product2' ");
mysql_query("UPDATE rooms4 SET fullhouse='\$gametotal'-'\$product3' ");

``````

hi
i tried

``````\$gametotal = \$product / (1 - \$profit)
``````