9 Replies - 2850 Views - Last Post: 11 September 2013 - 01:41 PM Rate Topic: -----

#1 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 30-January 13

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.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<style>
body{
background-color:lightblue;
}
h2{
color:darkblue;
}
td{
color:red;
}
</style>
<title>Welcome</title>
<script type="text/javascript">
/* <![CDATA[ */
function figureTip() {
     var bill = document.forms[0].bill.value;
     var tipPercent = document.forms[0].tip_percent.value;
     var numPeople = document.forms[0].num_people.value;
     var tipAmount = Math.round(bill * ("." + tipPercent));
     var total = Math.round(bill * (1 + "." + tipPercent));
     var totalPerPerson = Math.round(total / numPeople);
     document.forms[0].tip_amount.value = tipAmount.toLocaleString();
     document.forms[0].total.value = total.toLocaleString();
     document.forms[0].total_person.value = totalPerPerson.toLocaleString();
}
/* ]]> */
</script>
</head>
<body>

<h2>Tip Calculator</h2>
<form action="">
<table border="0">
<tr>
<td>Bill:</td>
<td><input type="text" name="bill" value="0" onchange="figureTip();" /></td>
</tr>
<tr>
<td>Tip %:</td>
<td><input type="text" name="tip_percent" value="0" onchange="figureTip();" /> (enter as a whole number)</td>
</tr>
<tr>
<td># of People:</td>
<td><input type="text" name="num_people" value="1" onchange="figureTip();" /> </td>
</tr>
<tr>
<td>Tip amount:</td>
<td><input type="text" name="tip_amount" value="0" readonly="readonly" /></td>
</tr>
<tr>
<td>Total:</td>
<td><input type="text" name="total" value="0" readonly="readonly" /></td>
</tr>
<tr>
<td>Total per Person:</td>
<td><input type="text" name="total_person" value="0" readonly="readonly" /></td>
</tr>
</table>
</form>
</body>
</html>


Is This A Good Question/Topic? 0
  • +

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

#2 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

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.
Was This Post Helpful? 0
  • +
  • -

#3 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 30-January 13

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.
Was This Post Helpful? 0
  • +
  • -

#4 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

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?
Was This Post Helpful? 0
  • +
  • -

#5 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 30-January 13

Re: Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 08:26 PM

View PostAtspulgs, 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
Was This Post Helpful? 0
  • +
  • -

#6 Atspulgs  Icon User is offline

  • D.I.C Regular

Reputation: 68
  • View blog
  • Posts: 380
  • Joined: 29-July 09

Re: Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 09:24 PM

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

Ive done this in two files, but it could be 1 as well.
File 1:
<html>
<head>
	<style>
		h2{color:black;}
		td{color:red;}
	</style>
	<title>Welcome</title>
</head>
<body>
	<h2>Tip Calculator</h2>
	<form action="res.php" method="post">
		<table border="0">
		<tr>
			<td>Bill:</td>
			<td><input type="text" name="bill" value="0" /></td>
		</tr><tr>
			<td>Tip %:</td>
			<td><input type="text" name="tip_percent" value="0"/> (enter as a whole number)</td>
		</tr><tr>
			<td># of People:</td>
			<td><input type="text" name="num_people" value="1" /> (enter as a whole number thats > 0)</td>
		</tr><tr>
			<td></td>
			<td><input type="submit" name="calculate" value="Calculate" /></td>
		</tr>
		</table>
	</form>
</body>
</html>


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.

Functions used:
isset()
is_numeric()
echo()
number_format()

$_POST is a superglobal btw.

I hope this helps, if you have any questions please do ask.

Edit: I hope the code isnt packed too tightly O.O

This post has been edited by Atspulgs: 10 September 2013 - 09:26 PM

Was This Post Helpful? 1
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,261
  • Joined: 08-June 10

Re: Looking for a tutorial to create simple tip calculator

Posted 10 September 2013 - 10:29 PM

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.
Was This Post Helpful? 0
  • +
  • -

#8 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 30-January 13

Re: Looking for a tutorial to create simple tip calculator

Posted 11 September 2013 - 01:22 PM

View PostAtspulgs, 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.

View PostDormilich, 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.

This post has been edited by Dormilich: 11 September 2013 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,261
  • Joined: 08-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#10 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 133
  • Joined: 30-January 13

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1