Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

subtracting a percentage from a figure

Status
Not open for further replies.

benluke4

Technical User
Jan 27, 2005
127
0
0
GB
Hi, Im trying to find out / learn how to subtract 10 % from a figure using PHP?

Im stuck

any ideas/hints/tips

Thanks

BEN
 
sorry, a number.

What im trying to do is

find out what 10% of $total is then subtract it from £total and give the answer.

 
ok i dont understand your answer,

This is what i have at the minute
Code:
<form action="" method="post">
<input name="percent" type="text" size="6" maxlength="10"> percent of 100
<br><br>
<input name="submit" type="submit" value="Calculate">
<input name="total" type="hidden" value="100">
</form>

<?php
$submit = $_POST['submit'];
$percent = $_POST['percent'];
$total = $_POST['total'];



// Simple
if ($submit == true) {
$ans = $percent / $total;

}

$ans = number_format($ans, 2, ',', ' ');

echo "<p>The answer is $ans</p>";

?>

I keep getting this warning message:

Warning: Division by zero in /home/wandptrainuk/public_html/test1.php on line 60


What i need to do is get rid of the warning message and subtract $ans from $total

Thanks for your help
 
Is your form in your PHP script?

If so, do something like:
Code:
<?php

// Simple
if (isset($_POST['submit']) {
$percent = $_POST['percent'];
$total = $_POST['total'];
$ans = $percent / $total;
$ans = number_format($ans, 2, ',', ' ');

echo "<p>The answer is $ans</p>";

}
?>

Also, there aren't 60 lines in the script you posted.

Ken
 
no my forms not in the php script

heres the full page
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title></title>
</head>
<form action="" method="post">
<input name="percent" type="text" size="6" maxlength="10"> percent of 100
<br><br>
<input name="submit" type="submit" value="Calculate">
<input name="total" type="hidden" value="100">
</form>

<?php
$submit = $_POST['submit'];
$percent = $_POST['percent'];
$total = $_POST['total'];



// Simple
if ($submit == true) {
$ans = $percent / $total;

}

$ans = number_format($ans, 2, ',', ' ');

echo "<p>The answer is $ans</p>";

?>

</body>
</html>
 
Still not 60 lines. There is only one place that I can see you doing division, so that is the only place you could get a div by 0 error. If that is the case then $total must be equal to 0 for some reason at that point. As a side note, the formula that you are using will not result in 10% less than the original value.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
This IS the full script.

Im not getting any error or wanring messages but its not working.

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title></title>
</head>


<form action="" method="post">
<input name="percent" type="text" size="6" maxlength="10"> percent of 100
<br><br>
<input name="submit" type="submit" value="Calculate">
<input name="total" type="hidden" value="100">
</form>

<?php
$submit = $_POST['submit'];
$percent = $_POST['percent'];
$total = $_POST['total'];



// Simple
if ($submit == true) {
$ans = $percent / $total;

}

$ans = number_format($ans, 2, ',', ' ');

$result = $total - $ans;

echo "<p>The answer is $result</p>";

?>

</body>
</html>
[code]

it keeps giveing answer as 100 what ever figure i type in

Thanks for the help
 
number_format returns a string, so you must do all math before you use it.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
benluke4:
Your script will not work because your script can't work the say you want it to.

You need to understand the flow of web-based apps. A script on a web server produces some HTML. That script then stops running. The browser renders the HTML and the user interacts with the GUI. An HTML form is submitted to another script which the processes the input.

The script that produces the form can be the script that processes the input from the submission of the form. But the script can only do one or the other, not both, during a single run of the script. This is the nature of web apps.

Typically, a script which will produce HTML on the first run and process the submission on the second takes the form:

Code:
<?php

if (isset ($_POST['foo'])
{
   //process the value in the form-field "foo"
   print '
   <html>
      <body>
         You submitted the value: ' . $_POST['foo'] . '
      </body>
   </html>';
}
else
{
   //produce the HTML form that includes a field named "foo"
   print '
   <html>
      <body>
         <form method="post" action="' . $_SERVER['PHP_SELF'] . '">
            <input type="text" name="foo"><br>
            <input type="submit">
         </form>
      </body>
   </html>';
}
?>

Your script is trying to both output the form and process the input during the same run.




Want the best answers? Ask the best questions!

TANSTAAFL!!
 
make two scripts one caled percent1.php and the second called percent2.php and cut and paste
percent1.php
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>do a percent</title>
</head>
<body>
	<form method="post" action="percent2.php" />
		<input name="percent" type="text" size="6" maxlength="10" /> percent of 100
		<br><br>
		<input name="total" type="hidden" value="100" />
		<input name="submitme" type="submit" value="Calculate" />
	</form>
</body>
</html>

percent2.php
Code:
<?php
if(isset($_POST['percent']))
{
	
	$percent = $_POST['percent'];
	$total = $_POST['total'];
	// Simple
	//this will give 90
	$ans =   ($total*$percent) ;
	//$ans = number_format($ans, 2, ',', ' ');
	
	
	
	echo '<p>The answer is '.$ans. '</p>';
}
echo '<a href="percent1.php">go back</a>';
?>


A problem with no solution is a problem viewed from the wrong angle
 
There was an error in a comment on the page as it says this will give you 90 and it should say this will give you 10, sorry if this confused the issue. I have left the script for you to finish off at the point where it gives the percent also the code you submitted had some errors in it eg no opening body tag, you might want to turn HTML errors on in your php ini file while you are developing your php.

A problem with no solution is a problem viewed from the wrong angle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top