Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Still learning php, must be an easier way

10:57 pm on Apr 15, 2011 (gmt 0)

New User

5+ Year Member

joined:Nov 2, 2009
posts: 13
votes: 0

I'll try to be as brief and to the point as possible as it might be a tad long. Thanks for taking a look.

User submits two values through a form.

Both values need to be confirmed correct (from a long set of values paired together), and if so, the post returns the corresponding value and also some computations; and if not, it simply returns a text error message, asking the user to try again.

$a = $_POST["a"];
$b = $_POST["b"];

// I'll just include two sets to give you an idea, but there will be several to check against, each four lines long
$in_01 = "test1"; // $a
$ip_01 = "1234"; // $b
$ia_01 = "200.00"; // return value
$tf_01 = "2.9"; // computational value
$in_02 = "test2"; // $a
$ip_02 = "1234"; // $b
$ia_02 = "400.00"; // return value
$tf_02 = "2.9"; // computational value

// Computation, I'll just include it for one set, repeats the same for all sets
$tp_01 = $ia_01*$tf_01/100; // return value * computational value
$tft_01 = round($tp_01,2); // simply formats the above computation to two decimal places
$total_01 = $ia_01+$tp_01; // return value + the computed value from two lines ago
$to_01 = round($total_01,2); // simply formats the above computation to two decimal places

// basically, if a and b match correctly, echo a few values (return value, computational value, and then the two computed values to two decimal places)
if ($a == $in_01 && $p == $ip_01) {echo "$ia_01, $tf_01, $tft_01, $to_01";} // set 01
elseif ($a == $in_02 && $p == $ip_02) {echo "$ia_02, $tf_02, $tft_02, $to_02";} // set 02
else {echo "error message";}

If the user inputs in the correct info and (for example) it corresponds to set 01, it would output like this:
200.00, 2.9, 5.80, 205.80

Now this probably isn't too bad, feels a little long, might be an easier way to display everything in two decimal places.

But that's just two sets, I'll probably want to have 50 sets.

Is there an easier way of doing this instead of repeating 50 four line sets, 200 lines of computation/decimal formatting, and a 50 line if statement?

Thanks for taking a look!
5:12 am on Apr 16, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 25, 2011
posts: 91
votes: 0

Use number_format to format numbers ;)

To work with 50 sets, you can use the "variable variables". Here's a short example, unrelated to your problem. Let's say you have 50 variables named $in1, $in2, $in3, etc. You can print them all using a loop like this:

for ($i = 1; $i <= 50; $i++)
echo ${"in$i"};
9:39 am on Apr 18, 2011 (gmt 0)

New User

5+ Year Member

joined:Nov 2, 2009
posts: 13
votes: 0

How about including a number_format or round() in the middle of an echo, is that possible?

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members