...

View Full Version : A simple calculator - Newbie.



COMMODORE64
03-10-2009, 11:52 PM
My friend is hosting the game server called roleplay.

So I'm little stumped on building a simple calculator for everyone to use.

Lately, I have been reading this (http://codingforums.com/showthread.php?t=134668), this given me some of idea, but I don't really have enough experience to build a php that will allows all of us to use it.

Here's what I want to build.

There should be a input that will be multiplies by time.

Input = Current wage
Time= Time interval
Output = Total of Money Earned

Time interval = 1 minute, 5, 10, 15, 20, 25, 30, 60, 90 and so forth.

There should be two button that clears the input and computes the input * output.

If someone is willing to help to build a php code in here, that would be great.

Many thanks.

Old Pedant
03-11-2009, 06:19 AM
Ummm...two questions:

(1) Why would you need heavyweight PHP for this when it can be so easily be done all in the browser in JavaScript code?

(2) If you really WANT a PHP answer, then why'd you post in a JavaScript forum???

COMMODORE64
03-11-2009, 06:56 AM
Hello,

It might be my mistake to post in wrong place.

A simple javascript will do just fine, hopefully my description is enough to ask someone to fill a javascript code for me :)

Philip M
03-11-2009, 08:33 AM
Here you are:-


Current Wage Per Minute: <input type = "text" id = "wage" size = "6">
Time Interval: <select id = "timeInt">
<option value = "0" selected>Select Time</option>
<option value = "5">5 minutes</option>
<option value = "10">10 minutes</option>
<option value = "15">15 minutes</option>
<option value = "20">20 minutes</option>
<option value = "25">25 minutes</option>
<option value = "30">30 minutes</option>
<option value = "60">60 minutes</option>
<option value = "90">90 minutes</option>
</select>
<br><br>
<input type = "button" value = "Calculate Total Money Earned" onclick = "calc()")>
<br><br>
Total Money Earned: <input type = "text" id = "earned" size = "8">

<script type = "text/javascript">
function calc() {
var wag = parseFloat(document.getElementById("wage").value);
if ((isNaN(wag) || wag < 0 || wag == "")) {
alert ("Invalid wage data!");
document.getElementById("wage").value = "";
document.getElementById("wage").focus();
return false;
}
var tim = document.getElementById("timeInt").value;
if (tim == 0) {
alert ("Please select a time interval!" )
return false;
}
var totwage = wag * tim;
document.getElementById("earned").value = totwage.toFixed(2);
}
</script>

This complies with what you asked for. If you really meant wage per hour or something you will need to change the code to divide the result by 60.


"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

COMMODORE64
03-11-2009, 04:59 PM
Phillip M,

Thank you for the code, but I will need the output to be shown without the input menu on time intervial as

5 = $
10= $
15= $
20= $
25= $

and so forth. the time intervial needed to be shown inside the table where people can read and decides what is the best for them when the type the number of wage, and hit the button to calculate the table of values.

Are you able to change it?

Many thanks

Philip M
03-11-2009, 05:37 PM
Sorry, I do not understand you. Please can you explain more clearly what you require as inputs and outputs, and what you mean by "table of values".

Do you mean that you want the user to enter a number (wage in dollars/and or cents per what?) and then the output is a table showing minutes at five-minute intervals and against each one the result of the calculation wage * minutes?


Current Wage Per Minute: <input type = "text" id = "wage" size = "6" onblur = "calc()">
<br><br>

or if you prefer a button change the above to:-
Current Wage Per Minute: <input type = "text" id = "wage" size = "6" >
<input type = "button" value = "Calculate Earnings" onclick = "calc()">
<br><br>

<table id="myTable" border="1">
<tr>
<td>MINUTES</td>
<td>TOTAL WAGE</td>
</tr>
</table>
<br>

<script type = "text/javascript">
var times = 0; // global variable records number of calculations
var earned = "";
function calc() {
var wag = parseFloat(document.getElementById("wage").value);
if ((isNaN(wag) || wag < 0 || wag == "")) {
alert ("Invalid wage data!");
document.getElementById("wage").value = "";
document.getElementById("wage").focus();
return false;
}

if (times == 0) { // create table on first calculation
for (var i = 100; i >= 5; i=i-5) {
earned = "$" + ((wag * i).toFixed(2));
var x = document.getElementById('myTable').insertRow(1);
var y = x.insertCell(0);
var z = x.insertCell(1);
y.innerHTML = i;
z.innerHTML = earned;
}
times ++;
}

else { // replace values in existing table
count = 1;
for (var i = 5; i <= 100; i=i+5) {
earned = "$" + ((wag * i).toFixed(2));
var x = document.getElementById('myTable').rows[count].cells;
x[0].innerHTML = i;
x[1].innerHTML = earned;
count ++;
}
}

}
</script>

Surely a hand calculator would be simpler for this?

COMMODORE64
03-12-2009, 08:34 AM
Phillips, thank you.

I have removed
or if you prefer a button change the above to:-
Current Wage Per Minute: <input type = "text" id = "wage" size = "6" >
<input type = "button" value = "Calculate Earnings" onclick = "calc()">
<br> and I found what I really want. You will be credited on this.

So many thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum