How to use proper switch or condition statement? - php

Can you help me solve this problem:
For that
$share Value <= 30000 the term is from 1 - 36 with 1.3% on the first
term and diminishing value of .65 on the next term while $share Value >= 30001 with diminishing value of .75
<?php
// Create a function that can accept parameters
function CalculateItem($share = 0,$terms = 0)
{
if($share <= 30000)
$multiplier = ($terms == "1")? .0130 : .0195;
elseif($share >= 30001)
$multiplier = ($terms == "1")? .0130 : .0205;
if(empty($multiplier))
return;
$data['share'] = $share;
$data['terms'] = $terms;
$data['interest'] = ($share * $multiplier);
$data['service_fee'] = ($share * .01);
$data['filling_fee'] = 30;
$data['cash_on_hand'] = $share - ($data['service_fee'] + $data['interest'] + $data['filling_fee']);
$data['loan_receivable'] = $data['cash_on_hand'] + $data['service_fee'] + $data['interest'] + $data['filling_fee'];
$data['debit'] = $data['loan_receivable'];
$data['credit'] = $data['interest'] + $data['service_fee'] + $data['filling_fee'] + $data['cash_on_hand'];
return $data;
}
// Get the state of data. Anything but false will trigger the breakout table
$data = (isset($_POST['calculate']))? CalculateItem($_POST['share'],$_POST['terms']) : false;
?>

While the rate is fixed you may do it as --
$rate=.0130;
if($share <= 30000)
$multiplier = $rate + ($terms-1)*(0.65);
elseif($share >= 30001)
$multiplier = $rate + ($terms-1)*(0.75);
By this way , you may calculate as per all terms

Related

I can't get my php to add all the things I need for it to add

I'm trying to add the if statements to the $totalReimb as well as the $totalMileReimb but can't seem to be able to get them to add only the $totalMileReimb is added
The variables
$ratePerMile = 0.35;
$miles = $_POST['miles'];
$bfast = $_POST['bfast'];
$lunch = $_POST['lunch'];
$dinner = $_POST['dinner'];
$hotel = $_POST['hotel'];
$totalMileReimb = $miles * $ratePerMile;
$totalReimb = 0 + $totalMileReimb;
$Yes = "Yes";
$No = "No";
The If Statements
if ($miles < 1 or $miles > 3000){
// input error message
print ("<p> Input Error, Try Again! </p>");
// link back to the form
print ("Return to form");
} else {
if ($miles >=10 or $miles <= 3000 );{
if ($bfast == $Yes) $totalReimb + 8.50;
if ($lunch == $Yes) $totalReimb + 12.50;
if ($dinner == $Yes)$totalReimb + 18.50;
if ($hotel == $Yes) $totalReimb + 110.00;
}
print ("<p> The Total Is $$totalReimb! </p>");
}
I think your error is this, $totalReimb + 12.50 should be one of the following:
$totalReimb = $totalReimb + 12.50
$totalReimb += 12.50.

php how to avoid negative answer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Hi I am a newbe and writing a simple code for tax purposes. So far I have wrote a small code but I think there seems to be a problem with the function taxable_i($i) which is not letting me make $taxable_i NIL. I have tried everything.
When I try to made $taxable_i NIL via function it wont let me do it. It shows me a result of £13900. I think the program is echoing $taxable_i rather then the tax payable.
If I delete the above all together then the tax payable comes to -£2199.80, which is in minus.
Please note that I put $taxable_i <personal_allowance so that the answer is zero but the result is not zero. I do not know why and what I am doing wrong.
Any help is most welcome.'
<?php
// VAT
define("vat_threshold",83000 );
define("vat_rate",0.2 );
// RELIEFS
define("personal_allowance",11000 );
//RATES
define("starting_rate",0 );
define("basic_rate",0.2 );
define("higher_rate",0.4 );
define("add_rate",0.45 );
define("divs_ord_rate",0.075 );
define("divs_uuper_rate",0.325 );
define("divs_add_rate",0.381 );
// THRESHOLDS
define("savings_income_band",5000 );
define("basic_rate_band",32000 );
define("higher_rate_band",150000 );
define("divs_allowance",5000 );
define ("band_0",0);
define("band_1",11000);
define("band_2",32000);
define("band_3",100000);
define("band_4",122000);
define("band_5",150000);
function taxable_i($i) {
if ($i <= band_1 ) {
$taxable_i = $i * 0;
return $taxable_i;
}
if ($i <= band_3 ) {
$taxable_i = $i - personal_allowance;
return $taxable_i;
}
if ($i >band_3 && $i <=band_4) {
$taxable_i = $i - (personal_allowance-($i - band_3)/2);
return $taxable_i;
}
if ($i > band_4) {
$taxable_i = $i;
return $taxable_i;
}
}
$starting_income = $i = 1;
echo $i;
$taxable_i = taxable_i($i);
echo $taxable_i;
switch ($taxable_i) {
case ($taxable_i > band_5):
$diff = $taxable_i - band_5;
$tax5 = $diff * add_rate;
$taxable_i = band_5;
$diff = $taxable_i - band_2;
$tax4 = $diff * higher_rate;
$taxable_i = band_2;
$tax3 = $taxable_i * basic_rate;
$tax_payable = $tax5 + $tax4 + $tax3;
break;
case ($taxable_i > band_2 && $taxable_i <= band_5 ):
$diff = $taxable_i - band_2;
$tax4 = $diff * higher_rate;
$taxable_i = band_2;
$tax3 = $taxable_i * basic_rate;
$tax_payable = $tax4 + $tax3;
break;
case ($taxable_i < band_2):
$tax = $taxable_i * basic_rate;
$tax_payable = $tax;
break;
default:
$taxable_i <= band_0;
$tax_payable == 0;
break;
}
echo $tax_payable;
?>
You don't put conditions in case statements, you should be using if/elseif/else there. The way switch/case works is that it compares the value of the initial switch () expression with the values of each case expression. So it's comparing $taxable_i to the value of $taxable_i > band_5, so it's comparing a number to true or false.
Replace the switch block with:
if ($taxable_i > band_5) {
$diff = $taxable_i - band_5;
$tax5 = $diff * add_rate;
$taxable_i = band_5;
$diff = $taxable_i - band_2;
$tax4 = $diff * higher_rate;
$taxable_i = band_2;
$tax3 = $taxable_i * basic_rate;
$tax_payable = $tax5 + $tax4 + $tax3;
}
elseif ($taxable_i > band_2 ) {
$diff = $taxable_i - band_2;
$tax4 = $diff * higher_rate;
$taxable_i = band_2;
$tax3 = $taxable_i * basic_rate;
$tax_payable = $tax4 + $tax3;
}
elseif ($taxable_i >= band_0) {
$tax = $taxable_i * basic_rate;
$tax_payable = $tax;
}
else {
$tax_payable == 0;
break;
}

Some rows aren't counted in a foreach loop

I get price from database with 9xx items.
I add this on show items page. Using Foreach $rows=$row. vprice is my sellingprice and dprice is my dealerprice
$commisionrate = 30;
$commisionfee = 100;
$fee = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$z = $x + $y;
$rate = $z;//(100*30%)+100
if (($rate > $row['vprice']) && ($fee < $row['vprice'])){
echo $fee;
}elseif (($fee > $row['vprice']) && ($rate < $row['vprice'])){
echo $rate;
}elseif ($row['dprice']=$row['vprice']){
echo $row['dprice'];
}
when I recheck all, I found that few items of $row['dprice'] is not counted and still show by old price. Example that is false: I found vprice is 188 with 80 dprice after calculate should be 104 but not changing with still stay on 80.
$commisionrate = 30;
$commisionfee = 100;
$fee = $row['dprice'] + $commisionfee;//+100
$x = $row['dprice'];
$y = $x * $commisionrate / 100;
$rate = $x + $y;
// You don't need to put nested brackets, it very simple condition
if ($rate > $row['vprice'] && $fee < $row['vprice']){
echo "fee: " . $fee; //add some hint words, so you know which condition fires
// You don't need to put nested brackets, it very simple condition
} elseif ($fee > $row['vprice'] && $rate < $row['vprice']) {
echo "rate: " . echo $rate;
// USE double `==`, because when single `=` used, the condition always returns true and you're confused by your result
} elseif ($row['dprice'] == $row['vprice']) {
echo "row[\'dprice\']: " . $row['dprice'];
// add last else
} else {
// this is helpful for debugging of your possible output, regardless you are awaiting some output here
}

else if not working in

if condition matches but else if condition is not working when i insert $total_salary = 10000 and $salary_type = NO.
if ($this->request->is('post')) {
$DATA = $this->data;
$employee_salary = $this->Salary->save($DATA);
$total_salary = $employee_salary['Salary']['salary'];
$salary_type = $employee_salary['Salary']['salary_in_ctc'];
echo $total_salary . $salary_type;
if (($total_salary > 15000) && ($salary_type === 'YES')) {
$pf_company = 1500;
$pf_employee = 1500;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
} elseif (($total_salary < 15000) && ($salary_type === 'NO')) {
echo'hello';
$pf_company = 1200;
$pf_employee = 1200;
$percent = 0.62;
$gross_salary = ($total_salary - $pf_company);
$base_salary = ($percent * $gross_salary);
$HRA = ($base_salary / 2);
$others = ($gross_salary - ($base_salary + $HRA));
$inhand_salary = ($gross_salary - $pf_employee);
}
try to match the uppercase condition and also check for white space in you conditions.
strtoupper(trim($salary_type))
The possible second issue could be:
As you are checking the datatype also in the condition $salary_type ==='NO' and in the post you stated you are passing $salary_type = NO. which doesn't seems to be a string type.
Please try removing one = from condition or passing NO as a string to validate the condition

Converting Javascript functions to PHP class

I have multiple JavaScript functions to calculate mayan horoscope sign, which I converted to a single PHP class. Everything works well, except some dates. For example, the date 06.10.1977 gives me a NULL result using the PHP class, but this date in the equivalent JavaScript function returns a proper value. Maybe I've got something wrong in PHP so, can somebody check this up for me?
JAVASCRIPT:
function leap_gregorian(year) {
return ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0)));
}
// GREGORIAN_TO_JD -- Determine Julian day number from Gregorian calendar date
var GREGORIAN_EPOCH = 1721425.5;
function gregorian_to_jd(year, month, day) {
return (GREGORIAN_EPOCH - 1) +
(365 * (year - 1)) +
Math.floor((year - 1) / 4) +
(-Math.floor((year - 1) / 100)) +
Math.floor((year - 1) / 400) +
Math.floor((((367 * month) - 362) / 12) +
((month <= 2) ? 0 : (leap_gregorian(year) ? -1 : -2)) +
day);
}
var MAYAN_COUNT_EPOCH = 584282.5;
// JD_TO_MAYAN_COUNT -- Calculate Mayan long count from Julian day
function jd_to_mayan_count(jd) {
var d, baktun, katun, tun, uinal, kin;
jd = Math.floor(jd) + 0.5;
d = jd - MAYAN_COUNT_EPOCH;
baktun = Math.floor(d / 144000);
d = mod(d, 144000);
katun = Math.floor(d / 7200);
d = mod(d, 7200);
tun = Math.floor(d / 360);
d = mod(d, 360);
uinal = Math.floor(d / 20);
kin = mod(d, 20);
return new Array(baktun, katun, tun, uinal, kin);
}
// JD_TO_MAYAN_HAAB -- Determine Mayan Haab "month" and day from Julian day
var MAYAN_HAAB_MONTHS = new Array("Pop", "Uo", "Zip", "Zotz", "Tzec", "Xul",
"Yaxkin", "Mol", "Chen", "Yax", "Zac", "Ceh",
"Mac", "Kankin", "Muan", "Pax", "Kayab", "Cumku", "Uayeb");
function jd_to_mayan_haab(jd) {
var lcount, day;
jd = Math.floor(jd) + 0.5;
lcount = jd - MAYAN_COUNT_EPOCH;
day = mod(lcount + 8 + ((18 - 1) * 20), 365);
return new Array(Math.floor(day / 20) + 1, mod(day, 20));
}
// JD_TO_MAYAN_TZOLKIN -- Determine Mayan Tzolkin "month" and day from Julian day
var MAYAN_TZOLKIN_MONTHS = new Array("Imix", "Ik", "Akbal", "Kan", "Chicchan",
"Cimi", "Manik", "Lamat", "Muluc", "Oc",
"Chuen", "Eb", "Ben", "Ix", "Men",
"Cib", "Caban", "Etznab", "Cauac", "Ahau");
var MAYAN_TZOLKIN_MONTHS_EN = new Array("Crocodile", "Wind", "House", "Lizard", "Serpent",
"Death", "Deer", "Rabbit", "Water", "Dog",
"Monkey", "Grass", "Reed", "Jaguar", "Eagle",
"Vulture", "Earth", "Knife", "Storm", "Flower");
function jd_to_mayan_tzolkin(jd) {
var lcount;
jd = Math.floor(jd) + 0.5;
lcount = jd - MAYAN_COUNT_EPOCH;
return new Array(amod(lcount + 20, 20), amod(lcount + 4, 13));
}
function getMayanSign(sign, month, day, year) {
var isValidated = true;
var result = "";
if (!IsNumeric(month.value) || month.value <= 0 || month.value > 12) {
month.value = "MM";
isValidated = false;
}
if (!IsNumeric(day.value) || day.value <= 0 || day.value > 31) {
day.value = "DD";
isValidated = false;
}
if (!IsNumeric(year.value) || year.value < 1900 ) {
year.value = "YYYY";
isValidated = false;
}
if (isValidated) {
year = new Number(year.value);
mon = new Number(month.value);
mday = new Number(day.value);
hour = min = sec = 0;
// Update Julian day
j = gregorian_to_jd(year, mon + 0, mday) +
(Math.floor(sec + 60 * (min + 60 * hour) + 0.5) / 86400.0);
maytzolkincal = jd_to_mayan_tzolkin(j);
result = MAYAN_TZOLKIN_MONTHS_EN[maytzolkincal[0] - 1];
} else result = "INVALID BIRTHDAY";
sign.value = result;
}
PHP CLASS:
class MayanCalculator {
// JD_TO_MAYAN_TZOLKIN -- Determine Mayan Tzolkin "month" and day from Julian day
private $MAYAN_TZOLKIN_MONTHS = array("Imix", "Ik", "Akbal", "Kan", "Chicchan",
"Cimi", "Manik", "Lamat", "Muluc", "Oc",
"Chuen", "Eb", "Ben", "Ix", "Men",
"Cib", "Caban", "Etznab", "Cauac", "Ahau");
private $MAYAN_TZOLKIN_MONTHS_EN = array("Crocodile", "Wind", "House", "Lizard", "Serpent",
"Death", "Deer", "Rabbit", "Water", "Dog",
"Monkey", "Grass", "Reed", "Jaguar", "Eagle",
"Vulture", "Earth", "Knife", "Storm", "Flower");
private $GREGORIAN_EPOCH = 1721425.5;
private $MAYAN_COUNT_EPOCH = 584282.5;
private $MAYAN_HAAB_MONTHS = array("Pop", "Uo", "Zip", "Zotz", "Tzec", "Xul",
"Yaxkin", "Mol", "Chen", "Yax", "Zac", "Ceh",
"Mac", "Kankin", "Muan", "Pax", "Kayab", "Cumku", "Uayeb");
private $_day;
private $_month;
private $_year;
private $sign;
private $signm;
function __construct($day, $month, $year) {
$this->_day = $day;
$this->_month = $month;
$this->_year = $year;
}
public function getMayanSign() {
$this->sign = $this->getSign();
$this->signm = $this->getMayanSignOnMayan();
$ids = new getIDS(null,null,$this->sign);
$id = $ids->returnIDS();
return array("mayan_sign" => $this->sign, "mayan_sign_on_mayan" => $this->signm, "mayan_sign_id" => $id["mayan_id"]);
}
private function getSign() {
$isValidated = true;
$result = null;
if (!is_numeric($this->_month) || $this->_month <= 0 || $this->_month > 12) :
$isValidated = false;
endif;
if (!is_numeric($this->_day) || $this->_day <= 0 || $this->_day > 31) :
$isValidated = false;
endif;
if (!is_numeric($this->_year) || $this->_year < 1900) :
$isValidated = false;
endif;
if ($isValidated) :
$hour = 0;
$min = 0;
$sec = 0;
//update julian day
$j = $this->gregorian_to_jd($this->_year, $this->_month+0, $this->_day) + (floor($sec + 60 * ($min + 60 * $hour) + 0.5) / 86400.0);
$maytzolkincal = $this->jd_to_mayan_tzolkin($j);
$result = $this->MAYAN_TZOLKIN_MONTHS_EN[$maytzolkincal[0]-1];
else :
$result = 'Wrong date '. $this->_day . '.' . $this->_month . '.' . $this->_year;
endif;
return $result;
}
private function getMayanSignOnMayan() {
$isValidated = true;
$result = null;
if (!is_numeric($this->_month) || $this->_month <= 0 || $this->_month > 12) :
$isValidated = false;
endif;
if (!is_numeric($this->_day) || $this->_day <= 0 || $this->_day > 31) :
$isValidated = false;
endif;
if (!is_numeric($this->_year) || $this->_year < 1900) :
$isValidated = false;
endif;
if ($isValidated) :
$hour = 0;
$min = 0;
$sec = 0;
//update julian day
$j = $this->gregorian_to_jd($this->_year, $this->_month+0, $this->_day) + (floor($sec + 60 * ($min + 60 * $hour) + 0.5) / 86400);
$maytzolkincal = $this->jd_to_mayan_tzolkin($j);
$result = $this->MAYAN_TZOLKIN_MONTHS[$maytzolkincal[0]]-1;
else :
$result = 'Wrong date '. $this->_day . '.' . $this->_month . '.' . $this->_year;
endif;
return $result;
}
private function leap_gregorian($year) {
return (($year % 4) == 0) && (!((($year % 100) == 0) && (($year % 400) != 0)));
}
private function gregorian_to_jd($year, $month, $day) {
$result = ($this->GREGORIAN_EPOCH - 1) +
(365 * ($year - 1)) +
floor(($year - 1) / 4) +
(-floor(($year - 1) / 100)) +
floor(($year - 1) / 400) +
floor((((367 * $month) - 362) / 12) +
(($month <= 2) ? 0 : ($this->leap_gregorian($year) ? -1 : -2)) +
$day);
return $result;
}
private function jd_to_mayan_count($jd) {
$jd = floor($jd) + 0.5;
$d = $jd - $this->MAYAN_COUNT_EPOCH;
$baktun = floor($d / 144000);
$d = $d % 144000;
$katun = floor($d / 7200);
$d = $d % 7200;
$tun = floor($d / 360);
$d = $d % 360;
$uinal = floor($d / 20);
$kin = $d % 20;
return array($baktun, $katun, $tun, $uinal, $kin);
}
private function jd_to_mayan_haab($jd) {
$jd = floor($jd) + 0.5;
$lcount = $jd - $this->MAYAN_COUNT_EPOCH;
$day = ($lcount + 8 + ((18 - 1) * 20)) % 365;
return array(floor($day / 20) + 1, $day % 20);
}
private function jd_to_mayan_tzolkin($jd) {
$jd = floor($jd) + 0.5;
$lcount = $jd - $this->MAYAN_COUNT_EPOCH;
return array(($lcount + 20) % 20, ($lcount + 4) % 13);
}
}
First: Always post all relevant script. Yours was missing the JavaScript function amod() which is crucial to solving your problem, I found it in the original here
Your PHP function jd_to_mayan_tzolkin() ist different from your JavaScript function jd_to_mayan_tzolkin():
While JavaScript uses a separate function amod() to calculate the modulus and returns the numerator if the modulus-result is 0
// AMOD -- Modulus function which returns numerator if modulus is zero
function amod($a, $b) {
return $this->mod($a - 1, $b) + 1;
}
your php function just returns the modulus which can be 0 and therefore the further processing of your PHP fails.
Add the following two functions to your class:
/* MOD -- Modulus function which works for non-integers. */
private function mod($a, $b) {
return $a - ($b * floor($a / $b));
}
// AMOD -- Modulus function which returns numerator if modulus is zero
private function amod($a, $b) {
return $this->mod($a - 1, $b) + 1;
}
And change the PHP method jd_to_mayan_tzolkin() as follows:
private function jd_to_mayan_tzolkin($jd) {
$jd = floor($jd) + 0.5;
$lcount = $jd - $this->MAYAN_COUNT_EPOCH;
return array($this->amod(($lcount + 20),20), $this->amod(($lcount + 4),13));
}

Categories