/    Sign up×
Community /Pin to ProfileBookmark

Php Math and Display question

I have a reservation system that books trips. Well I need to be able to calculate the total price if any combination of variables are present.
There are 4 main variables:
*Adult Oneway – *Adult Roundtrip – *Child Oneway – *Child Roundtrip
This is what I have to assign each of those a value:

[code=php]$pricea1 = 39.00; //adult oneway
$pricear = 70.00; //adult roundtrip
$pricec1 = 30.00; //child oneway
$pricecr = 52.00; //child roundtrip
$total1 = number_format($pricea1 * $adult); //total price if adult oneway
$total2 = number_format($pricear * $adult); //total price if adult roundtrip
$total3 = number_format($pricec1 * $child); //total price if child oneway
$total4 = number_format($pricecr * $child); //total price if child roundtrip
$total5 = number_format($total1 + $total2); //total price if adult oneway + adult roundtrip
$total6 = number_format($total1 + $total3); //total price if adult oneway + child oneway
$total7 = number_format($total1 + $total4); //total price if adult oneway + child roundtrip
$total8 = number_format($total2 + $total3); //total price if adult roundtrip + child oneway
$total9 = number_format($total2 + $total4); //total price if adult roundtrip + child roundtrip[/code]

Now I need to be able to calculate these and display them in a single input text field.
It needs to be something like this:

[code=php]if ($adult && $trip == “Oneway”) {?><input type=”text” name=”chargetotal” value='<?php echo (“$total1”);?>.00′><?
}
else if ($adult && $trip == “Roundtrip”) {?><input type=”text” name=”chargetotal” value='<?php echo (“$total2”);?>.00′><?
}
else if ($adult && $trip && $child && $trip == “Oneway”) {?><input type=”text” name=”chargetotal” value='<?php echo (“$total5”);?>.00′><?
}
else if ($adult && $trip + $child && $trip == “Roundtrip”) {?><input type=”text” name=”chargetotal” value='<?php echo (“$total6”);?>.00′><?
}[/code]

But as you can see, that’s very complicated and there must be an easier way to calculate these totals and have them display in a single text field. The way I am trying above would call for way to many “if’s and else if’s”.

Any ideas?
Thanks

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Feb 14.2007 — Something like this?[code=php]<?php
$num_adult = 2;
$num_child = 1;
$fare_type = 1 // 1 = One-way; 2 = Roundtrip

$pricea[1] = 39.00; //adult oneway
$pricea[2] = 70.00; //adult roundtrip
$pricec[1] = 30.00; //child oneway
$pricec[2] = 52.00; //child roundtrip

$total = ($pricea[$fare_type] * $num_adult) + ($price[$fare_type] * $num_child);
echo "<input type='text' name='chargetotal' value='" . number_format($total,2) . "'>";
?>[/code]
×

Success!

Help @launchpad67a spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...