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

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 />');
}

Try this -

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

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

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

I've had a go;

$total = 200;
$perpage = 10;
$end = $total / $perpage;

for ($n=1;$n<=$end;$n++) {
    $n1 = ($n * $perpage) - 1;
    echo "$n,$n1<br/>";
}

What about something like

<?php
$total = 200;
$increase_by = 10;

for ($left = 1, $right = $increase_by; $right <= $total; $left = $left + $increase_by, $right = $right + $increase_by) {
    printf('%d, %d<br>', $left, $right);
}

But, as MatsLindh indeed says, if you are using this to create offset/limit combinations, you probably want to keep your limit the same all the time, because "LIMIT 0,10" means "fetch 10 records, starting at row 0" while "LIMIT 10,20" means "fetch 20 records, starting at row 10". You probably want "LIMIT 10,10" instead, saying "fetch 10 records, starting at row 10".

Let's say you want to display 10 records per page, and you're on page 2, you would need a LIMIT clause that says: LIMIT 10,10, meaning what I mentioned above. The offset is 10, because the first page had offset 0, making the query fetch the first 10 records.

Therefore all you need to fetch the records on the current page is the page number and the number of records you want to fetch.

If you want to calculate how many pages you have: ceil($number_of_records / $records_per_page);

try it,

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