HiDayurie Dave
HiDayurie Dave

Reputation: 1807

PHP pagination customize page number

How to customize the pagination page number below?

Current:

enter image description here

and what I want is:

1 2 3 4 5 Next Last Page

Here is the PHP code:

for($i=1; $i<=$Num_Pages; $i++)
{
    if($i != $Page)
    {
        echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
    }
    else
    {
        echo "<b> $i </b>";
    }
}

Any clue how to do this?

Upvotes: 2

Views: 316

Answers (2)

A. L
A. L

Reputation: 12639

Could try this:

$Num_Pages = 70;

//$Page = $_GET['Page'];
$Page = 12;
$from = $Page - ($Page % 5) + 1;

for($i=$from; $i<$from+5 && $i<$Num_Pages; $i++)
{
    if($i != $Page)
    {
        echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
    }
    else
    {
        echo "<b> $i </b>";
    }
}
$next_page = $Page+1;
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$next_page '>next</a> ]";
echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$Num_Pages'>Last Page</a> ]";

Replace the $Page = 12 with the $_GET to get the page dynamically.

Haven't really tested it.

Upvotes: 1

Nomura Nori
Nomura Nori

Reputation: 5167

$start_page = intval($Page / 5) *5 + 1;  
$to_page = $start_page + 4;
$is_last_group = ($start_page + 5)> $Num_Pages);
if (is_last_group) 
   $to_page = $Num_Pages;  
for($i = $start_page; $i <= to_page; $i++)
    {
        if($i != $Page)
        {
            echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i'>$i</a> ]";
        }
        else
        {
            echo "<b> $i </b>";
        }
    }
if($page < $Num_Pages){
       $next_page = $page + 1;
       echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$next_page '>next</a> ]";
       echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$Num_Pages'>last</a> ]";
}

Upvotes: 1

Related Questions