tags:

views:

63

answers:

2

Overall goal: User enters some number in minutes and I change those numbers from minutes to some usable form in a calculation.

How do I convert the user input in the form of minutes to some number or value that I can use later on in this php script?

<?php
$minutes = $_REQUEST["minutes"];
// We initialize the total to $1.99 for the connection fee: 
$total  = 1.99;
// For each additional minute we add $0.45?
// For example if  3 minutes are used the cost = $2, 
$addmin = 0.45;
/* How do I convert the user inputted minutes to some value
 I can manipulate with php so that I can just add via php and give the user 
 a cost, for example,  3minutes entered into a form might be = cost of $2.
 What do I need to read or study?*/
//We use a if statement here:
if (filter_has_var(INPUT_POST, "minutes"))
{
// If the user selects the number of minutes, we echo out the number of minutes.
print $minutes;
print "mins.";
// Condition pending  here: 
$total += filter_input(INPUT_POST, "$addmin");
}
print "<p>The total cost is: \$$total</p> \n";
?>
A: 

why don't you just convert everything in seconds and use the number of seconds and the cost per seconds to do any computation ? When you are done you can convert again from seconds to minutes and display your results.

wezzy
How exactly would that help? what's the difference between working with minutes and working with seconds? If the calculation is based on cost per minute it would be much easier to just work with minutes.
Andy E
+2  A: 

Minutes are already a usable form of number for a calculation. If you use the $minutes variable in a calculation it should automatically be cast to a number.

// $minutes = 3, $addmin = 0.45, 3 * 0.45 = 1.35
$subtotal = $minutes * $addmin;
$total   += $subtotal;

If you wanted the first 3 minutes to be $2 and then each additional minute to be $0.45, you'd do something like the following:

$subtotal = 0;
// If less than 3 minutes were input, use 3 minutes anyway.
$minutes  = $minutes < 3 ? 3 : $minutes;  
if ($minutes >= 3)  // If minutes
{
    $minutes -= 3;
    $subtotal = 2;
}

// multiple the remaining minutes by the additional cost
$subtotal += $minutes * $addmin;
$total    += $subtotal;
Andy E
You can reduce this code further by eliminating the ternary line and just adding an else to the if.
iddqd
Good spot, thanks.
Andy E
Dude that's impressive, I really appreciate the comments, how do you develop your algorithms like that? I mean do you write down on a piece of paper each step the program is suppose to take or you just start coding? Apparently just coding is not working out really good for me, I need to get better at pro-grammatically stepping through the solutions for a given problem, again thanks.
Newb
The more you code the more this sort of thing will just come naturally to you. Of course, there's always going to be algorithms that are just really difficult and you need some sort of plan before attacking it. If you know what you want the end result to be, it's usually just a case of jotting down some pseudo code or scenarios figuring out the best way to tackle the problem.
Andy E