[ACCEPTED]-Add pagination in wordpress admin in my own customized plugin-wordpress

Accepted answer
Score: 42

Easy Steps :

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

Find total numbers of records

$limit = 10; // number of rows in page
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var( "SELECT COUNT(`id`) FROM {$wpdb->prefix}table_name" );
$num_of_pages = ceil( $total / $limit );

Give 1 limit:

$entries = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}table_name LIMIT $offset, $limit" );

Add this code where you want pagination:

$page_links = paginate_links( array(
    'base' => add_query_arg( 'pagenum', '%#%' ),
    'format' => '',
    'prev_text' => __( '«', 'text-domain' ),
    'next_text' => __( '»', 'text-domain' ),
    'total' => $num_of_pages,
    'current' => $pagenum
) );

if ( $page_links ) {
    echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
Score: 7

Generic pagination function.

This example looks like this on a large 2 db:

<< Previous 1 2 3 4 5 6 7 8 9 ... 822 823 Next >>

or

<< Previous 1 2 ... 815 816 817 818 819 820 821 822 823 Next >>

or

<< 1 Previous 1 2 ... 812 813 814 815 816 817 818 ... 822 823 Next >>

enjoy...

<?php 
/*Max Number of results to show*/
$max = 10;
/*Get the current page eg index.php?pg=4*/

if(isset($_GET['pg'])){
    $p = (int) $_GET['pg'];
}else{
    $p = 1;
}

$limit = ($p - 1) * $max;
$prev = $p - 1;
$next = $p + 1;
$limits = (int)($p - 1) * $max;

//This is the query to get the current dataset (change api to suit)
$result = mysqli_query($res, 'SELECT * from yourtable limit '.$limits.','.$max.'');

//Get total records from db (change api to suit)
$totalres = mysqli_result($res, mysqli_query($res, 'SELECT COUNT(id) AS tot FROM yourtable"'),0);

//devide it with the max value & round it up
$totalposts = ceil($totalres / $max);
$lpm1 = $totalposts - 1;

echo pagination($totalposts, $p, $lpm1, $prev, $next);

//The function
function pagination($totalposts, $p, $lpm1, $prev, $next){
    $adjacents = 3;
    if($totalposts > 1)
    {
        $pagination .= "<center><div>";
        //previous button
        if ($p > 1)
        $pagination.= "<a href=\"?pg=$prev\"><< Previous</a> ";
        else
        $pagination.= "<span class=\"disabled\"><< Previous</span> ";
        if ($totalposts < 7 + ($adjacents * 2)){
            for ($counter = 1; $counter <= $totalposts; $counter++){
                if ($counter == $p)
                $pagination.= "<span class=\"current\">$counter</span>";
                else
                $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";}
        }elseif($totalposts > 5 + ($adjacents * 2)){
            if($p < 1 + ($adjacents * 2)){
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
                    if ($counter == $p)
                    $pagination.= " <span class=\"current\">$counter</span> ";
                    else
                    $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";
                }
                $pagination.= " ... ";
                $pagination.= " <a href=\"?pg=$lpm1\">$lpm1</a> ";
                $pagination.= " <a href=\"?pg=$totalposts\">$totalposts</a> ";
            }
            //in middle; hide some front and some back
            elseif($totalposts - ($adjacents * 2) > $p && $p > ($adjacents * 2)){
                $pagination.= " <a href=\"?pg=1\">1</a> ";
                $pagination.= " <a href=\"?pg=2\">2</a> ";
                $pagination.= " ... ";
                for ($counter = $p - $adjacents; $counter <= $p + $adjacents; $counter++){
                    if ($counter == $p)
                    $pagination.= " <span class=\"current\">$counter</span> ";
                    else
                    $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";
                }
                $pagination.= " ... ";
                $pagination.= " <a href=\"?pg=$lpm1\">$lpm1</a> ";
                $pagination.= " <a href=\"?pg=$totalposts\">$totalposts</a> ";
            }else{
                $pagination.= " <a href=\"?pg=1\">1</a> ";
                $pagination.= " <a href=\"?pg=2\">2</a> ";
                $pagination.= " ... ";
                for ($counter = $totalposts - (2 + ($adjacents * 2)); $counter <= $totalposts; $counter++){
                    if ($counter == $p)
                    $pagination.= " <span class=\"current\">$counter</span> ";
                    else
                    $pagination.= " <a href=\"?pg=$counter\">$counter</a> ";
                }
            }
        }
        if ($p < $counter - 1)
        $pagination.= " <a href=\"?pg=$next\">Next >></a>";
        else
        $pagination.= " <span class=\"disabled\">Next >></span>";
        $pagination.= "</center>\n";
    }
    return $pagination;
}
?>
Score: 5

I solved this problem in different way. And 7 solution is here

I use second option which has 6 title "How To Add Pagination To WordPress 5 Plugin?". This gives almost same pagination 4 functionality as WordPress has.

Do not forget 3 to download that "pagination.class.php" file, put 2 it in your plugin folder and give appropriate 1 path while including this file in your plugin.

More Related questions