对于PHP的循环逻辑问题

时间:2012-06-27 09:44:22

标签: php logic

这是我的新代码,它几乎正常工作,但我觉得我的while循环错了一些在哪里?

与之前相同,让用户生病将其放入var中。然后根据服务年限获得用户权利,然后计算他们所做的事情并查看用户是否已使用所有完整权利,然后查看他们是否已经使用了所有那半工资权利。

请帮助?

if($this->CURRENT_USER['User']['role_id'] > 3) { //locks out user types

        //Get Holidaytypes
        $types = $this->Holiday->find(
            'all',
            array(
             'conditions' => array(
                                 'Holiday.holidaystype_id' => 3,
                                 'Holiday.user_id' => $id
             )));

        //Get starting date
        $contracts = $this->Holiday->User->Contract->find(
            'all',
            array(
                'conditions' => array(
                'Contract.user_id' => $id//$data['user_id']),
                'order' => array('Contract.startson' => 'ASC')
            )
        );
    //Get How Many sick days
    foreach ($types as $key => $value) {
            global $SickTotal; 
            $typesDataEnds =  strftime ("%u-%d-%Y", $types[$key]['Holiday']['endson']);
            $typesDataStarts = strftime ("%u-%d-%Y", $types[$key]['Holiday']['startson']);

            $SickTotal = count($typesDataEnds - $typesDataStarts);  
            //echo $SickTotal;

        //Get Contract Start & End Dates
        $start = array_shift($contracts);
        $end = array_pop($contracts);

        $endDate = $end['Contract']['endson'];
        $startDate = $start['Contract']['startson'];

        if (empty($endDate)) {
                $endDate = time('now');
        }
        if (!empty($startDate)) {
            $SortEnd = strftime("%Y", $endDate);
            $SortStart = strftime("%Y", $startDate);
            $YearsService = $SortEnd - $SortStart;

            if ($YearsService <= 1) {
                    $SetFullEntitlement = 5;
                    $SetHalfEntitlement = 5;
                    //echo 'one year';
            } elseif ($YearsService >= 2) {
                    $SetFullEntitlement = 10;
                    $SetHalfEntitlement = 10;
                    //echo 'two years';
            } elseif ($YearsService >= 5) {
                    $SetFullEntitlement = 20;
                    $SetHalfEntitlement = 20;
                    //echo 'up to five years';
            } elseif ($YearsService > 5) {
                    $SetFullEntitlement = 30;
                    $SetHalfEntitlement = 30;
                    //echo 'five years or more';
            } else {
                    $SetFullEntitlement = 0;
                    $SetHalfEntitlement = 0;
                    //echo 'no sick pay';
            }
        } else {
            $YearsService = 0;
            //echo 'Sorry No Start Date For You Found!';
        }

        while ($SickTotal > 0) {
            if ($SetFullEntitlement != 0) {
                $SetFullEntitlement--;
            } elseif ($SetHalfEntitlement != 0) {
                $SetHalfEntitlement--;
            }
        }

        echo 'FullPay:';
        echo $SetFullEntitlement;
        echo '<br/><br/>Halpay:';
        echo $SetHalfEntitlement;
        echo $SickTotal;
    } 

        debug($types);
        die();  
        //$this->render('/artists/holidayslist');

    }       
}

2 个答案:

答案 0 :(得分:1)

If ($startdate <= 1 year) {

如果这就是你输入的内容,它就不会起作用了。也许strtotime可能会对它有所了解吗?

无论如何,

 For ($i = $Totalsick, $i >= $Fulldays, $i--) {
    For ($i = $Totalsick, $>= $Halfdays, $i--) {

你在那里错过了一个$ i - 你正在评估$ Halfdays。你也使用$ i进行两个单独的循环,所以它们都在同一个计数器上。切换你的第二个循环使用另一个变量。

答案 1 :(得分:0)

我不太确定我是否完全按照您要在此处执行的操作,但您可以稍微整理一下代码并节省代码行。我假设$startdate是多少年前用户开始的。

$sickdays= array( 0=>5, 1=>10, 2=>20, 5=>30 );
$userdays= 0;
foreach ( array_keys( $sickdays ) as $years_service )
{
   if ( $years_service <= $startdate )
      $userdays=$sickdays[$years_service];
}

现在$userdays应该是此用户的正确付费病假天数和半天(两者在您的示例中始终相同)。一个简单的比较应足以检查他们是否花费的时间少于或超过他们分配的天数和半天。

我没有尝试过这个,因为我今天没有使用PHP,所以我确定有人会直接拍摄我,但是通过设置一次变量并减少行数,您会发现代码更容易维护