Simple Pagination

Here’s a (dodgy) PHP function to do simple pagination. Pass it big array of data, or a mysql resource, and it gives back a trimmed down version of the data along with the HTML links to place under your data table.

Example below:

<?php

// OPTION #1 - paginate data from a mysql query:
// first grab all our data to display.
$sql = "SELECT * FROM `big_data_table`";
$big_result = mysql_query($sql);
// pass it into the pagination, want 100 items per page:
$pagination_result = dodgy_pagination($big_result, 100);
// display our table with the 100 rows, and our nice pagination links at the bottom:
echo '<table>';
foreach($pagination_result['rows'] as $row){
    echo '<tr>';
    echo '<td>' . $row['field1'] . '</td>';
    echo '<td>' . $row['field2'] . '</td>';
    echo '</tr>';
}
echo '</table>';
echo $pagination_result['links'];


// OPTION #2 - paginate data from a big php array
$big_array = array(
    array('field1'=>'foo','field2'=>'bar'),
    array('field1'=>'foo','field2'=>'bar'),
    array('field1'=>'foo','field2'=>'bar'),
    //.... etc...
);
// pass it into the pagination, want 10 items per page:
$pagination_result = dodgy_pagination($big_array, 10);
// display our table with the 10 rows, and our nice pagination links at the bottom:
echo '<table>';
foreach($pagination_result['rows'] as $row){
    echo '<tr>';
    echo '<td>' . $row['field1'] . '</td>';
    echo '<td>' . $row['field2'] . '</td>';
    echo '</tr>';
}
echo '</table>';
echo $pagination_result['links'];



function dodgy_pagination($rows,$per_page = 50){
    $page_number = $_REQUEST['pg'];
    $data = array();
    $data['rows']=array();
    $data['links']='';
    if($per_page<=0){
        $per_page = 20;
    }
    $db_resource = false;
    if(is_resource($rows)){
        // have the db handle for the sql query
        $db_resource = $rows;
        unset($rows);
        $rows = array();
        $total = mysql_num_rows($db_resource);
        if($total<=$per_page){
            $rows = query_to_array($db_resource,false);
        }
    }else if(is_array($rows)){
        // we have the rows in an array.
        $total = count($rows);
    }else{
        echo 'Pagination failed. Sorry!';
        exit;
    }
    if($total<=$per_page){
        // double up on query rows from above. oh well.
        $data['rows']=$rows;
    }else{
        $pg=0;
        $page_number = min(ceil($total/$per_page)-1,$page_number);
        // slice up the result into the number of rows requested.
        if($db_resource){
            // do the the mysql way:
            mysql_data_seek($db_resource, ($page_number*$per_page));
            $x=0;
            while($x < $per_page){
                $row_data = mysql_fetch_assoc($db_resource);
                if($row_data){
                    $data['rows'] [] = $row_data;
                }
                $x++;
            }
            unset($row_data);
        }else{
            // the old array way.
            $data['rows']=array_slice($rows, ($page_number*$per_page), $per_page);
        }
        $data['links']='&nbsp;';
        $request_uri = preg_replace('/[&?]pg=d+/','',$_SERVER['REQUEST_URI']) . '&';
        if(count($data['rows'])){
            $page_count = ceil($total/$per_page);
            // group into ranges with cute little .... around the numbers if there's too many.
            $rangestart = max(0,$page_number-5);
            $rangeend = min($page_count-1,$page_number+5);
            ob_start();
            if($rangestart>0){
                ?> <a href="<?=$request_uri;?>pg=0" rel="0" class="">1</a> <?php
                if($rangestart>1)echo ' ... ';
            }
            for($x=$rangestart;$x<=$rangeend;$x++){
                if($x == $page_number){
                    ?>
                    <a href="<?=$request_uri;?>pg=<?=$x;?>" rel="<?=$x;?>" class="current">
<?=($x+1);?>
</a>
                    <?php
                }else{
                    ?>
                    <a href="<?=$request_uri;?>pg=<?=$x;?>" rel="<?=$x;?>" class="">
<?=($x+1);?>
</a>
                    <?php
                }
            }
            if($rangeend < ($page_count-1)){
                if($rangeend < ($page_count-2))echo ' ... ';
                ?>
<a href="<?=$request_uri;?>pg=<?=($page_count-1);?>"
rel="<?=($page_count-1);?>" class=""><?=($page_count);?></a>
<?php
            }
            $data['links']=ob_get_clean();
        }
    }
    return $data;
}

 

Very dodgy way of doing it, much better if you query the database for the actual information you need, instead of getting ALL the data into php and then slicing it up.

Leave a Reply

Your email address will not be published. Required fields are marked *