如果函数用于数字计算,则反转长PHP

时间:2016-10-26 20:48:36

标签: php math

我正在申请使用PHP和mysql记录和处理公司请假记录。在我的申请中,我必须处理三个主要的休假类型,如下所示。

causal - 7 
annual - 14 
medical - 7

在我的数据库中,我有一个名为leaves的表,它分别对这三列及其值进行控制,如下所示

---name----casual----annual----medical
---jhon------7---------14--------7---
---mary------7---------14--------7---
---bill------5----------3--------0---

当员工申请休假PHP功能时,请检查可用的休假计数并从这三个值中减去应用的休假编号

如果休假类型是临时的,休假计数为15,则应减去临时,医疗和年假,以满足员工休假要求。我设法实施了这一部分。下面是我的代码

if ($ltype == "medical")
{
if ($medical >= $hmd)
    {
    $medical-= $hmd;

    }
  else
    {
    $tval = $hmd - $medical;
    $tval2 = $hmd - $tval;
    $medical-= $tval2;

    if ($casual >= $tval && $tval > 0)
        {
        $casual-= $tval;

        }
      else
        {
        $tval3 = $tval - $casual;
        $tval4 = $tval - $tval3;
        $casual-= $tval4;

        if ($annual >= $tval3 && $tval3 > 0)
            {
            $annual-= $tval3;

            }
          else
            {
            $tval5 = $tval3 - $annual;
            $tval6 = $tval3 - $tval5;
            $annual-= $tval6;

            $nopay += $tval5;


            }
        }
    }
}  

我有三个代码集用于三种休假类型,上面的代码按照我的意愿进行数学运算。但我的问题是,有时用户想要将这些减去的假期重新添加到休假表中。当涉及到这个时,我必须计算此函数反向,以获得从第一个函数的每列减去的确切数字。然后我计划计算添加每列减去叶子到休假记录末尾的叶子数量。但是当逆转时这也不起作用,因为那些记录没有按任何顺序处理。

我想实现将这些叶子重新添加到叶子表而没有任何错误的方法。

我知道我的方法可能不适合这类问题,这是非常基本的编程问题。有人可以解释一下我如何完全按照相同的方式反转我的第一个功能,或者我应该遵循哪些其他编程概念来解决这些类型的问题。

1 个答案:

答案 0 :(得分:0)

好的,假设$ hmd是天数......这应该会让你非常接近:

http://sandbox.onlinephpfunctions.com/code/f16d899de4318a89e1d80e8737c49b079a5ee179

<?php

$ltype = 'medical';
$casual = 7;
$annual = 15;
$medical = 7;
$nopay = 0;
$hmd = 9;

echo "medical $medical, casual $casual, annual $annual, nopay $nopay\n";

if ($ltype == "medical") {
  if ($medical >= $hmd) {
    $medical -= $hmd;
  } else {
    // sorry no more med leave
    $rem = $hmd - $medical;
    $medical = 0;

    if ($casual >= $rem && $rem > 0) {
      $casual -= $rem;
    } else {
      // sorry no more cas leave
      $rem2 = $rem - $casual;
      $casual = 0;

      if ($annual >= $rem2 && $rem2 > 0) {
        $annual -= $rem2;
      } else {
        // sorry now you owe us money 
        $nopay += ($rem2 - $annual);
        $annual = 0; 
      }
    }
  }
} 

echo "medical $medical, casual $casual, annual $annual, nopay $nopay\n";

// need to set these to know when to "roll" to the next leave type...
$max_casual = 7;
$max_annual = 15;
$max_medical = 7;

if ($ltype == "medical") {
  $rem = 0; // remainder after paying back nopay
  if ($nopay >= $hmd) {
    $nopay -= $hmd;
    // well, we put some back but you still owe us!
  } else {
    // alright you paid it back!
    $rem = $hmd - $nopay;
    $nopay = 0;
  }

  if ($rem > 0) {
    // let's restore some annual
    $annual += $rem;
    if ($annual > $max_annual) {
      $rem = $annual - $max_annual;
      $annual = $max_annual;
    } else {
      $rem = 0;
    }
  }

  if ($rem > 0) {
    // let's restore some casual
    $casual += $rem;
    if ($casual > $max_casual) {
      $rem = $casual - $max_casual;
      $casual = $max_casual;
    } else {
      $rem = 0;
    }
  }

  if ($rem > 0) {
    // whoa you're rolling in days off 
    $medical += $rem;
    if ($medical > $max_medical) {
      $rem_not_possible = $medical - $max_medical;
      $medical = $max_medical;
    } else {
      $rem = 0;
    }
  }
} 

echo "medical $medical, casual $casual, annual $annual, nopay $nopay\n";