Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation A simple calculator - Newbie.

    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, 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.
    Last edited by COMMODORE64; 03-10-2009 at 11:53 PM. Reason: Edited = Forgot to add something.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    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???

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,986
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Here you are:-

    Code:
    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)
    Last edited by Philip M; 03-11-2009 at 09:19 AM. Reason: Improved

  • #5
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,986
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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?

    Code:
    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?
    Last edited by Philip M; 03-11-2009 at 08:26 PM.

  • Users who have thanked Philip M for this post:

    COMMODORE64 (03-12-2009)

  • #7
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Phillips, thank you.

    I have removed
    Code:
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •