Basic PHP calculator

This is a discussion on Basic PHP calculator within the Discussion of PHP scripting (alt.comp.lang.php) forum.

Basic PHP calculator

Postby Phil on Thu Dec 04, 2008 1:05 am

Hello Guys

I've used php in the past for some html forms and found it a bit of a
nightmare tbh.

I'm looking for a script that works out how many pack of laminate
flooring it takes to cover a room.

So the equation would be (x*y)/a = number of packs.

The user types in the length (x) and width(y) of a room and also the
coverage of one pack of flooring (a).

And the script works out how many packs the user would need and diplays
it on the page.

Can someone point me in the right direction to make a start?

Phil
Phil
 
Posts: 82
Joined: Sun Jul 27, 2003 3:32 am

Re: Basic PHP calculator

Postby Phil on Thu Dec 04, 2008 1:51 am


>
> Can someone point me in the right direction to make a start?
>
> Phil

I've made a start and got a working version using the code below, I
could do with a amendment to the code to round up the $total to a full
number.

$x = $_POST['width'];
$y = $_POST['length'];
$a = $_POST['packsize'];
?>
You entered width:

You entered length:

You entered pack size:

$total = $x * $y / $a;
echo "$total" ?>
Phil
 
Posts: 82
Joined: Sun Jul 27, 2003 3:32 am

Re: Basic PHP calculator

Postby Phil on Thu Dec 04, 2008 2:04 am

PHIL wrote:
I could do with a amendment to the code to round up the $total to a full
> number.
>
> > $x = $_POST['width'];
> $y = $_POST['length'];
> $a = $_POST['packsize'];
> ?>
> You entered width:

> You entered length:

> You entered pack size:

> > $total = $x * $y / $a;
> echo "$total" ?>

Found it using

echo ceil("$total")

I'm quite happy with myself.
Phil
 
Posts: 82
Joined: Sun Jul 27, 2003 3:32 am

Re: Basic PHP calculator

Postby Trookat on Thu Dec 04, 2008 2:10 am

PHIL wrote:
>
>>
>> Can someone point me in the right direction to make a start?
>>
>> Phil
>
> I've made a start and got a working version using the code below, I
> could do with a amendment to the code to round up the $total to a full
> number.
>
> > $x = $_POST['width'];
> $y = $_POST['length'];
> $a = $_POST['packsize'];
> ?>
> You entered width:

> You entered length:

> You entered pack size:

> > $total = $x * $y / $a;
> echo "$total" ?>

Phil ,I spent a whole minute thinking how to respond to this.

Many here will be quick to say that what you have posted smells like a
homework assignment . Unfortunately I'm one of them , but I'm going to
be nice.

You may be learning php in a class or just fiddling around a home , but
anyone who wants to learn something really should start looking at the
manual or use google

the manual got php is available at http://php.net/

lets start with what you said 'round' so i looked it up in the manual
and found round , then after reading abit i notice a functions called
floor and ceil are listed in the related functions bit.

ceil is used for rounding up
floor is used for rounding down

Please do some legwork first before posting.
If you have tried to find something and failed then simply say . " hi i
was looking for X but after some searching on the internet I could not
find a solution ", because even the best of us can fail to find
something because we used the wrong key words when using google

regards trookat.
Trookat
 
Posts: 85
Joined: Sat Aug 23, 2003 4:54 am

Re: Basic PHP calculator

Postby Trookat on Thu Dec 04, 2008 2:14 am

PHIL wrote:
> PHIL wrote:
> I could do with a amendment to the code to round up the $total to a full
>> number.
>>
>> >> $x = $_POST['width'];
>> $y = $_POST['length'];
>> $a = $_POST['packsize'];
>> ?>
>> You entered width:

>> You entered length:

>> You entered pack size:

>> >> $total = $x * $y / $a;
>> echo "$total" ?>
>
> Found it using
>
> echo ceil("$total")
>
> I'm quite happy with myself.

well done good to see ! You replied faster then i could type a response
, ignore my inner cynic in the last response and accept handclaps of
approval. good work!

Trookat
 
Posts: 85
Joined: Sat Aug 23, 2003 4:54 am

Re: Basic PHP calculator

Postby Phil on Thu Dec 04, 2008 2:26 am


>>
>> I'm quite happy with myself.
>
> well done good to see ! You replied faster then i could type a response
> , ignore my inner cynic in the last response and accept handclaps of
> approval. good work!
>

Hi

Thanks for your comment, can I ask a qestion or two?

I'm getting a warning message 'Warning: Division by zero ' from this line

$total = $x * $y / $a;

Can this be bypassed?

I also want the html form and the php result displyed onthe same page,
I'm using the 'post' command which reloads the page, is there a better
solution?

Phil
Phil
 
Posts: 82
Joined: Sun Jul 27, 2003 3:32 am

Re: Basic PHP calculator

Postby Janwillem Borleffs on Thu Dec 04, 2008 2:39 am

PHIL schreef:
> I'm getting a warning message 'Warning: Division by zero ' from this line
>
> $total = $x * $y / $a;
>
> Can this be bypassed?
>

Ensure that $a != 0

> I also want the html form and the php result displyed onthe same page,
> I'm using the 'post' command which reloads the page, is there a better
> solution?
>

No, unless you're willing to use techniques like ajax.


JW
Janwillem Borleffs
 
Posts: 2077
Joined: Mon Jul 21, 2003 3:29 pm

Re: Basic PHP calculator

Postby Phil on Thu Dec 04, 2008 3:41 am

Janwillem Borleffs wrote:
> PHIL schreef:
>> I'm getting a warning message 'Warning: Division by zero ' from this line
>>
>> $total = $x * $y / $a;
>>
>> Can this be bypassed?
>>
>
> Ensure that $a != 0
>
>> I also want the html form and the php result displyed onthe same
>> page, I'm using the 'post' command which reloads the page, is there a
>> better solution?
>>
>
> No, unless you're willing to use techniques like ajax.
>
>
> JW

Sorry but I have no idea what to do or where to put $a != 0
Phil
 
Posts: 82
Joined: Sun Jul 27, 2003 3:32 am

Re: Basic PHP calculator

Postby Janwillem Borleffs on Thu Dec 04, 2008 4:38 am

PHIL schreef:
> Sorry but I have no idea what to do or where to put $a != 0

You will need to brush up your basic programming skills:

if ($a != 0) {
$total = $x * $y / $a;
} else {
$total = 0;
// Handle condition
}


JW
Janwillem Borleffs
 
Posts: 2077
Joined: Mon Jul 21, 2003 3:29 pm

Re: Basic PHP calculator

Postby Curtis on Fri Dec 05, 2008 2:50 am

On Thu, 04 Dec 2008 08:51:12 GMT, pbryd@hotmail.com wrote:
>
> >
> > Can someone point me in the right direction to make a start?
> >
> > Phil
>
> I've made a start and got a working version using the code below, I
> could do with a amendment to the code to round up the $total to a full
> number.
>
> > $x = $_POST['width'];
> $y = $_POST['length'];
> $a = $_POST['packsize'];
> ?>
> You entered width:

> You entered length:

> You entered pack size:

> > $total = $x * $y / $a;
> echo "$total" ?>

You might want to consult various tutorials, especially concerning
security, if you continue to deal with Web app programming.

Here are a few improvements to help catch errors caused by user input
and sanitize user input:

// keep track of errors
$error = array();

// get and sanitize data, making sure it exists first
$x = isset($_POST['width']) ? floatval($_POST['width']) : 0;
$y = isset($_POST['length']) ? floatval($_POST['length']) : 0;
$a = isset($_POST['packsize']) ? floatval($_POST['packsize']) : 1;
$total = 0;

// calculate data, provided we don't divide by 0
if ($a == 0)
$error[] = 'Error: division by zero.';
else
$total = ceil($x * $y / $a);

// check if form was submitted and that we have no errors
// if we do have errors, just output them and don't calculate
if (!empty($_POST) && !$error) {
echo "Width: $x
\n"
. "Length: $y
\n"
. "Pack size: $a
\n"
. "Total: $total\n";
} else {
// list error(s)
echo "
    \n";
    foreach ($error as $msg)
    echo "\t
  • $msg
  • \n";
    echo "
\n";
}
?>

A little note about isset() and floatval(): it is good practice to
check that an array index exists before using it, so we use isset()
to check. We use floatval() to ensure that we don't output the user
input unsanitized. If they decided to use HTML characters, for
whatever reason (XSS, in some cases), your version would output them
directly. floatval() serves to return anything not numeric as 0.

Good luck.

--
Curtis
$email = str_replace('sig.invalid', 'gmail.com', $from);
Curtis
 
Posts: 387
Joined: Mon Nov 27, 2006 3:39 am


Return to Discussion of PHP scripting (alt.comp.lang.php)

Who is online

Users browsing this forum: No registered users and 0 guests