duanrui3480 2014-12-10 08:52
浏览 24
已采纳

PHP编号范围

I'm having a complete mental block on how to do this..

I have 'x' DB entries. I want to list them in blocks of 'y'.

I'm starting to try and do this just with numbers within PHP, then I'll consider the DB side.

$total = '200';
$perpage = '10';

So I'm trying to get the following output:

1,10
11,20
21,30
31,40
41,50 
etc

If $perpage was 20, then the results would be :

1,20
21,40
41,60
etc

This is what I've got so far.. and it's way off !

$total = '200';
$page = '10';
$base = '1';

echo $total / $page."</br>";

$list = $total / $page;

for ($n=1;$n<=$list;$n++) {
    echo "$n,".$n * $page."<br/>";
}

Can some one point me in the right direction for doing this. Forget the DB, I@m interested in doing this with number as above to start with.

Thanks

  • 写回答

5条回答 默认 最新

  • dsl36367 2014-12-10 09:00
    关注

    When querying the DB as a LIMIT you're usually interested in OFFSET and ROWS, meaning that you want 0, 10, 20, 30, 40, 50, while ROWS stay at 10: LIMIT 0, 10, LIMIT 10, 10, etc. If you're going to use it in a query, you're approaching it from the wrong side.

    To generate the sequence in your question, keep the offset and perpage separate from your counter.

    $perpage = 10;
    $offset = 1;
    $total = 200;
    
    for ($i = 0; $i < $total; $i += $perpage) 
    {
        print(($i + $offset) . ', ' . ($i + $perpage) . '<br />');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?