Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 06:06 PM

Hi I am just looking through tuts right now and I see there are a million on how to create a calculator but does anyone know where I can find one that shows how to create a tip calculator not just a regular calculator. I am not asking anyone to write the code just point me in the direction of a tut if possible because I can find them for vb.net, c+ and Javascript which I already know how to make one for Javascript but I do not know how to in php although I assume it is similar thanks. I have posted my code for a Javascript one, but now I am wanting to write one in php.

Replies To: Looking for a tutorial to create simple tip calculator

Re: Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 06:27 PM

I actually will have a go at this one myself, but can I ask you, How come you need a tutorial if youve already done the problem? Like ok yeah, in a different language, but still, wouldnt it be at least somewhat similar?

Anyhow, I like the problem and I havent made a tip calculator before, so I plan to do it myself. I'll bump the thread if I think i can give some pointers.

Re: Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 07:01 PM

Thank you the thing for me really is that I know it would be similar but it has been driving me crazy because I am fairly new to php I know more about vb.net and Javascript and just wanted to try it but can not figure it out.

Re: Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 08:07 PM

Im not too good at teaching things, but I can give you a commented code perhaps, would that be similar to a tutorial for you? Perhaps you can just ask about the issues youre encountering when trying to write it?

Re: Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 08:26 PM

Atspulgs, on 10 September 2013 - 10:07 PM, said:

Im not too good at teaching things, but I can give you a commented code perhaps, would that be similar to a tutorial for you? Perhaps you can just ask about the issues youre encountering when trying to write it?

The commented code would be great Thank you very much. Once I see it I will understand a bit better and with the comments I can see how you did it which would greatly help me to understand how it works. That is the main reason I look for tutorials a lot of the time because that way your not just getting the code but actually understanding how it works. Thanks again

Thats a simple form, just html that points to the php file which then takes care of the calculations.

file 2: res.php

<?php
//Checks if all the required data has been acquired
if(isset($_POST['bill'], $_POST['tip_percent'], $_POST['num_people'])){
//Checks if the data is numeric as it should be
if(is_numeric($_POST['bill']) && is_numeric($_POST['tip_percent']) && is_numeric($_POST['num_people'])){
echo "Bill: ".$_POST['bill'];
if($_POST['tip_percent']<0) // Making sure that percentage doesnt go into negatives
$_POST['tip_percent'] = 0; //If it does, set it to 0
echo "<br/>Tip %: ".$_POST['tip_percent'];
if($_POST['num_people']<1) //Just making sure this number isnt less than 1
$_POST['num_people'] = 1; //If it is, set it to 1 which is the default
echo "<br/>People: ".$_POST['num_people'];
//echo "Tip amount: ".($_POST['bill'] * ('.'.$_POST['tip_percent'])); <- Cool solution but doesnt work with decimal points.
echo "<br/>Tip amount: ".$tip = number_format($_POST['bill'] * $_POST['tip_percent'] / 100, 2);
echo "<br/>Total: ".$total = $tip+$_POST['bill'];
echo "<br/>Total per Person: ".$total/$_POST['num_people'];
}else echo "At least one of the values was not a number!";
}else echo "At least one of the values was missing!";
?>

Now that is the php code that works with the data. When it comes to sending data from client to server, we mostly use POST, however you have to make sure youre pointing your form to the right file that can handle the method of the form.

Re: Looking for a tutorial to create simple tip calculator

Posted 11 September 2013 - 01:22 PM

Atspulgs, on 10 September 2013 - 11:24 PM, said:

Ok, I literally took your javascript code and turned it into php, with some tweaks ofc.

Thank you works great and I understand a bit better how it works. I am now trying to tweak it a bit and make it all one file I know I have to do this by appending the php and html files into one file but I am a little rusty with this lol so I will work on it as a bit of a challenge I guess thanks so much for your help.

Dormilich, on 11 September 2013 - 12:29 AM, said:

tip calculators in PHP have the disadvantage that you need a roundtrip to the server to get the result, that’s probably why there is no tutorial.

Well that makes sense I could not understand why I could not find it at first lol.

Re: Looking for a tutorial to create simple tip calculator

Posted 11 September 2013 - 01:33 PM

Quote

I am now trying to tweak it a bit and make it all one file I know I have to do this by appending the php and html files into one file

why?

usually you try hardest to separate it all. there is really no benefit of having one huge file. imagine if you have to change something in any of the languages concerned, itís just too easy to accidentally break one of the other languages.

Re: Looking for a tutorial to create simple tip calculator

Posted 11 September 2013 - 01:41 PM

lol actually I guess you are right the more I think about it. I was thinking it may be easier to manage as one file but yeah actually it would just make it harder to go through so I see your point thanks.