在一定数量的行开始不同的计数 - php模数

时间:2013-09-11 19:38:21

标签: php mysql modulus

我有一个出租车应用程序,其中骑行时间插入表中。每个n(例如5个)骑都是免费的。我需要的是当用户有超过50次骑行时,每3次应该是免费的。 在下面的代码中,我设法每隔5进行一次,但是当它超过50时,它开始计算所有游乐设施中的每3个,并且一切都被捣碎了。是否可以计算每5到50,然后每3个计数一次?

    $i=$rows;
    if ($rows>50){
        if ($i != 0 && $i%3 == 0){
        echo "<p>This ride is free</p>";
        }
        elseif ($i%3 == 2){
        echo "<p>Next ride is free</p>";
        }
    $i--;
    break;
    }
    elseif ($rows<=50){
        if ($i != 0 && $i%5 == 0){
        echo "<p>This ride is free</p>";
        }
        elseif ($i%5 == 4){
        echo "<p>Next ride is free</p>";
        }
    $i--;
    break;
    }

3 个答案:

答案 0 :(得分:2)

我认为以下是您正在寻找的内容。我把$i--;break;取出来是因为我无法看到它们的目的,除非这是在一个更大的循环中,但我仍然不理解它们的目的。反正。

$i=$rows;
if ($rows>50){
    $i = $i - 50;
    if ($i != 0 && $i%3 == 0){
        echo "<p>This ride is free</p>";
    } else if ($i%3 == 2){
        echo "<p>Next ride is free</p>";
    }
} else if ($rows<=50){
    if ($i != 0 && $i%5 == 0){
        echo "<p>This ride is free</p>";
    } else if ($i%5 == 4){
        echo "<p>Next ride is free</p>";
    }
}

http://sandbox.onlinephpfunctions.com/

上进行测试
for($rows=0;$rows<75;$rows++){
  $i=$rows;
  echo $rows;
  if ($rows>50){
      $i = $i - 50;
      if ($i != 0 && $i%3 == 0){
          echo ": This ride is free";
      } else if ($i%3 == 2){
          echo ": Next ride is free";
      }
  } else if ($rows<=50){
      if ($i != 0 && $i%5 == 0){
          echo ": This ride is free";
      } else if ($i%5 == 4){
          echo ": Next ride is free";
      }
  }
  echo "\n";

结果:

0
1
2
3
4: Next ride is free
5: This ride is free
6
7
8
9: Next ride is free
10: This ride is free
11
12
13
14: Next ride is free
15: This ride is free
16
17
18
19: Next ride is free
20: This ride is free
21
22
23
24: Next ride is free
25: This ride is free
26
27
28
29: Next ride is free
30: This ride is free
31
32
33
34: Next ride is free
35: This ride is free
36
37
38
39: Next ride is free
40: This ride is free
41
42
43
44: Next ride is free
45: This ride is free
46
47
48
49: Next ride is free
50: This ride is free
51
52: Next ride is free
53: This ride is free
54
55: Next ride is free
56: This ride is free
57
58: Next ride is free
59: This ride is free
60
61: Next ride is free
62: This ride is free
63
64: Next ride is free
65: This ride is free
66
67: Next ride is free
68: This ride is free
69
70: Next ride is free
71: This ride is free
72
73: Next ride is free
74: This ride is free

答案 1 :(得分:0)

那些休息是什么;声明? 这个代码在循环中吗? 如果是,那么循环中的行$i=$rows;也是吗?因为如果是这样,$ rows将始终是&gt; 50。

您应该将整个循环代码发布到问题中,以便我们可以获得更好的视图。

答案 2 :(得分:0)

使用此代码,第53,第56等游乐设施应该是免费的:

    if ($rows>50){
    if ($i != 0 && ($i - 50) %3 == 0){