使用php创建数字系列

时间:2013-06-28 16:48:59

标签: php series

我有一个表格,有两个日期提货和退货日期现在我想根据天数收费,但我陷入了循环。

我想收费如果天1,2,3,4比5,6,7,8免费再收费9,10,11,12再次收取13,14,15,16再收费像这样如何编写这个模式的代码,我已经尝试过代码,但没有尝试

所以我可以另外增加费用。

if($rental_days%5==0 && $rental_days <10): 

           $charge = ($rental_days-1)*$result['rent'];
         elseif($rental_days%6==0):
          $charge = ($rental_days-2)*$result['rent'];
          elseif($rental_days%7==0):
         $charge = ($rental_days-3)*$result['rent'];

         elseif($rental_days%5==0 && $rental_days >8):  
             $charge = ($rental_days-3)*$result['rent']+$result['rent'];


        else:
         $charge = ($rental_days-4)*$result['rent']+$result['rent']; 

        endif;  

3 个答案:

答案 0 :(得分:1)

你不是在看四组,而是实际上是8组...你收费的8分的前半部分,你没有的下半部分,所以你想要除以8,看看是否剩余是4或更少...

if ( $rental_days % 8 <= 4 && $rental_days % 8 != 0 )

无论我们有多少天,这都会有效(例如:如果已经有300天了)。你不会想要使用循环,因为这可以永远持续下去。

答案 1 :(得分:1)

我将重新解释你的问题:你正在寻找一个函数/算法,给定数字N,将返回以下内容(在这里制作一个小表):

 N  return
 1     1
 2     2
 3     3
 4     4
 5     4
 6     4
 7     4
 8     4
 9     5
10     6

以下代码演示了如何执行此操作的简单方法 - 仅当相同数量的付费天数(4)后面跟有相同数量的无薪天数(4)时才会生效:

<?php
for($n=1; $n<20; $n++)
  {
  $m = ($n-1)%4 + 1;               // returns 1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4...
  $unpaid = intval(($n-1)/4)%2;    // returns 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,...
  $fourBlocks = intval(($n-1)/8);  // returns 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,...
  if ($unpaid == 1)
    {
    $daysCharged = $n - $m - 4 * $fourBlocks;
    }
  else
    {
    $daysCharged = $n - 4 * $fourBlocks;
    }
  echo $n . " days: " . $daysCharged . " days charged, ". ($n - $daysCharged) . " free\n";
  }
?>

这会产生以下输出:

1 days: 1 days charged, 0 free
2 days: 2 days charged, 0 free
3 days: 3 days charged, 0 free
4 days: 4 days charged, 0 free
5 days: 4 days charged, 1 free
6 days: 4 days charged, 2 free
7 days: 4 days charged, 3 free
8 days: 4 days charged, 4 free
9 days: 5 days charged, 4 free
10 days: 6 days charged, 4 free
11 days: 7 days charged, 4 free
12 days: 8 days charged, 4 free
13 days: 8 days charged, 5 free
14 days: 8 days charged, 6 free
15 days: 8 days charged, 7 free
16 days: 8 days charged, 8 free
17 days: 9 days charged, 8 free
18 days: 10 days charged, 8 free
19 days: 11 days charged, 8 free

注意 - 在更一般的情况下,如果您有N天的费用,然后是M天没有收费,您可以按如下方式更改代码:

<?php
$N = 4;
$M = 4;
echo $N . " days charged followed by " . $M . " days free:\n";
for($days=1; $days<20; $days++)
  {
  $numBlocks = intval(($days - 1) / ($N + $M)); // a "block" is a complete cycle of paid & unpaid days
  $remainder = $days - ($N + $M) * $numBlocks; // the "remainder" contains an incomplete cycle
  $unpaid = $remainder - $N;
  if ($unpaid < 0) $unpaid = 0;  // this now contains the number of unpaid days in the incomplete cycle
  $daysCharged = $numBlocks * $N + $remainder - $unpaid;  // easy to compute the number that must be charged
  echo $days . " days: " . $daysCharged . " days charged, ". ($days - $daysCharged) . " free\n";
  }
?>

这实际上更加强大,无论M和N的值如何,都能为您提供正确的答案(因此您可以非常轻松地更改收费方案)。

答案 2 :(得分:0)

这是你的解决方案,这正是你想要的。看看这个,希望它对你有用..

<?php
    $charge = $counter = 40;
    $switch = 0;
    for($i=1;$i<=31;$i++)
    {
        if($switch == 0)
        {
            echo $i." => Charge = ".$charge."</br>";
            $charge = $charge+$counter;
        }
        else if($switch == 1)
        {
            echo $i." => NoCharge = ".$charge."</br>";
        }
        if($i%4 == 0)
        {
            if($switch == 0)
            {
                $switch = 1;
                if($i%8 == 0)
                {
                    $charge = $charge+$counter;
                }
            }
            else if($switch == 1)
            {
                $switch = 0;
                if($i%8 == 0)
                {
                    $charge = $charge+$counter;
                }
            }
        }
    }
 ?>

这是输出

1 => Charge = 40
2 => Charge = 80  
3 => Charge = 120
4 => Charge = 160
5 => NoCharge = 200
6 => NoCharge = 200
7 => NoCharge = 200
8 => NoCharge = 200
9 => Charge = 240
10 => Charge = 280
11 => Charge = 320
12 => Charge = 360
13 => NoCharge = 400
14 => NoCharge = 400
15 => NoCharge = 400
16 => NoCharge = 400
17 => Charge = 440
18 => Charge = 480
19 => Charge = 520
20 => Charge = 560
21 => NoCharge = 600
22 => NoCharge = 600
23 => NoCharge = 600
24 => NoCharge = 600
25 => Charge = 640
26 => Charge = 680
27 => Charge = 720
28 => Charge = 760
29 => NoCharge = 800
30 => NoCharge = 800
31 => NoCharge = 800