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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度