开发者

Paginate an array

I have an array $myarray; I want to paginate the array content, here is what i am thinking of doing:

//Find total number of rows
        $all_rs = $newArrayOfRows;
        $this->total_rows = count($all_rs);


        //Max number of pages
        $this->max_pages = ceil($this->total_rows / $this->ro开发者_运维百科ws_per_page );
        if ($this->links_per_page > $this->max_pages) {
            $this->links_per_page = $this->max_pages;
        }


        //Check the page value just in case someone is trying to input an aribitrary value
        if ($this->page > $this->max_pages || $this->page <= 0) {
            $this->page = 1;
        }


        //Calculate Offset
        $this->offset = $this->rows_per_page * ($this->page - 1);


        //Fetch the required result set

How do i join all this info in order to fetch from my initial array just the info i need for every single page? Ty very much


I am guessing from your variable $all_rs that your array is a database result set.

If so, you should fetch only the rows you want to display on that given page from the database.

You should not fetch every row in the table then only display some of them. Once your site has some real traffic, doing so will be too much for your server to handle and the whole site will stop functioning. Asking for more data than you need...

  1. Wastes memory holding all the results you'll never use
  2. Wastes time/bandwidth transferring them from the database server to PHP
  3. Wastes CPU time finding the rows you're not going to use
  4. Wastes disk IO reading rows you're not going to use from the hard drives
  5. Wwastes some more RAM by filling up the query cache with few large result sets instead of many smaller ones.

To get only the rows you need, use a LIMIT clause in your SQL query, which lets you specify a number of rows to retrieve and an offset (how many rows to skip). The logic is essentially the same as you're coming up with now to figure out how to slice your array. Instead, you use it to figure out what numbers to put in the LIMIT part of your database query.


$array = array_slice($all_rs, $this->offset, $this->rows_per_page);

So $array will contain the desired subset.


Array slice should do this.

array array_slice(array, offset[, length]);
array: Base array to be used
int offset: Starting element index
int length (optional):

But are you sure you want to copy your data around like that?

you might just want an accessor that gives you item one the page likes this

$item = c->getItem($pageNumber,$itemNumber);


It looks like $newArrayOfRows is mine. ;)

//Find total number of rows
$all_rs = $newArrayOfRows;
$this->total_rows = count($all_rs);


//Max number of pages
$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
if ($this->links_per_page > $this->max_pages) {
    $this->links_per_page = $this->max_pages;
}


//Check the page value just in case someone is trying to input an aribitrary value
if ($this->page > $this->max_pages || $this->page <= 0) {
    $this->page = 1;
}


//Calculate Offset
$this->offset = $this->rows_per_page * ($this->page - 1);


//Fetch the required result set

for($n = $this->offset;$n < $this->rows_per_page;$n++){
    $thisPageArray[] = $newArrayOfRows[$n];
}

//NOW $thisPageArray HAS THE ROWS OF THIS PAGE.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜