Hi Friends,

I am trying to limit the number of pages to display on my code. Can someone please help?
I am trying to get something like http://stackoverflow.com/questions/8361808/limit-pagination-page-number

$Num_Rows = mysql_num_rows($objQuery);




$Per_Page = 15;   // Per Page



$Page = $_GET["Page"];
if(!$_GET["Page"])
{
$Page=1;
}

$Prev_Page = $Page-1;
$Next_Page = $Page+1;

$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}





$strSQL .=" order by adid  DESC LIMIT $Page_Start , $Per_Page  ";
$objQuery  = mysql_query($strSQL);

?>

<?
while($objResult = mysql_fetch_array($objQuery))
{
?>


<div style="margin-left:64px; margin-right:64px;"  >




 <span class='urllink'>  <a href="viewad.php?adid=<?=$objResult['adid'];?>"> <?=$objResult["title"];?>  - <?=$objResult["age"];?> </a> <span>    (<?=$objResult["location"];?>) <br>  <br> <b>Service Type:</b> <?=$objResult["servicetype"];?> <br> <b>Height:</b> <?=$objResult["height"];?> <br> <b>Body Type:</b> <?=$objResult["bodytype"];?>

  <hr  width="64%" align="left"/>

</div>
<?
}
?>
</table>

<div style="margin-left:58px; margin-right:58px;  padding:6px; background-color:#FFF;  font-size:20px; margin-top: 60px; text-shadow: 2px 2px 3px gray  ">
<span class="paginationNumbers">
<?
if($Prev_Page)
{
echo  " &nbsp;<a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]&category=$_GET[category]'  class='pagNumActive' ><< Back</a>  &nbsp; ";
}

for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "&nbsp; <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]&category=$_GET[category]'  class='pagNumActive'>$i</a>&nbsp; ";
}
else
{
echo " <span class='pagNumActivelink'> $i </span> ";
}
}
if($Page!=$Num_Pages)
{
echo "&nbsp; <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]&category=$_GET[category]' class='pagNumActive'  >Next>></a>&nbsp; ";
}

mysql_close($objConnect);
}
?>

Dani AI

Generated

a simple way to cap visible page links is to render a small sliding window around the current page, plus optional first/last links with ellipses. Also validate the incoming Page value and clamp it to the available range so you never render invalid links. The snippet below does that and preserves existing query params (like your txtKeyword/category) when building URLs. It also computes the total pages with ceil() so you do not get an extra empty page. (php.net)

<?php
$perPage    = 15;
$totalRows  = (int) $Num_Rows;

$page = filter_input(INPUT_GET, 'Page', FILTER_VALIDATE_INT,
         ['options' => ['default' => 1, 'min_range' => 1]]);

$totalPages = max(1, (int) ceil($totalRows / $perPage));
$page       = min($page, $totalPages);

// windowed pagination (e.g., 7 numbers max)
$window = 7;
$half   = (int) floor($window / 2);
$start  = max(1, $page - $half);
$end    = min($totalPages, $start + $window - 1);
$start  = max(1, $end - $window + 1); // readjust left edge

function page_url($p) {
    $params = $_GET;
    $params['Page'] = $p;
    return $_SERVER['PHP_SELF'] . '?' . http_build_query($params);
}

echo '<nav class="pagination">';
if ($page > 1) echo '<a rel="prev" href="' . page_url($page - 1) . '"><< Back</a>';

if ($start > 1) {
    echo '<a href="' . page_url(1) . '">1</a>';
    if ($start > 2) echo '<span class="gap">...</span>';
}
for ($i = $start; $i <= $end; $i++) {
    echo $i == $page ? '<span aria-current="page">'.$i.'</span>'
                     : '<a href="'.page_url($i).'">'.$i.'</a>';
}
if ($end < $totalPages) {
    if ($end < $totalPages - 1) echo '<span class="gap">...</span>';
    echo '<a href="' . page_url($totalPages) . '">' . $totalPages . '</a>';
}
if ($page < $totalPages) echo '<a rel="next" href="' . page_url($page + 1) . '">Next >></a>';
echo '</nav>';

Hook it up to your query with: $offset = ($page - 1) * $perPage; then ... ORDER BY adid DESC LIMIT $offset, $perPage. Remember MySQL offsets start at 0. (dev.mysql.com)

Note: your sample uses mysql_* which was deprecated in PHP 5.5 and removed in PHP 7. Switch to mysqli or PDO and use prepared statements (you can bind LIMIT values safely as integers). (php.net)

is right that the linked post has the idea; the pattern above is a drop-in that keeps the UI compact and robust.

The link you posted has all the code you need, so what's the problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.