duankeye2342 2012-06-20 01:50
浏览 198
已采纳

使用PHP获得下一个增量

I have a value that increases by 1 every 5 views. The MySQL table looks like:

myvalue views increment
10      12    5

For instance, once the views reaches 15, myvalue will change to 11.

I acomplish this by doing:

$div = $myvalue / $increment;
if (ceil($div) == $div) {
    $myvalue = $myvalue + $increment;
}

What I am trying to do is do a quick calculation that would tell me how many more views to go before the next increment. In this example, I would like to return the number 3, since there are 3 more views until the next time myvalue hits an increment of 5, which is 15.

If myvalue was 10, views was 26 and increment was 25, I need to get the number 24, since there are 24 more views needed before we hit the next increment of 25, which is 50.

Any ideas how to do this?

  • 写回答

2条回答 默认 最新

  • douyuan4697 2012-06-20 02:00
    关注

    I think you can use this formula

    $diff = $increment - ($views % $increment);
    

    And also you original code could be written as

    if(($views % $increment) === 0)
    {
      $myvalue += 1; // you said myvalue will increase by one
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏