PHP Pagination for 1,2,3... vs 1,2,3,4,5,etc I am having an issue where I am doing a query and it is coming back with about 100 pages, I was hoping to have it come back with 1,2,3...30,31...99,100 instead of 1,2,3,4,5,6,7,8,etc. please see below for working code that returns back results as unwanted format, I am new here so if this question is written wrong please let me know and I will make necessary changes , thanks in advance:

function getPage($stmt, $pageNum, $rowsPerPage)
{
$offset = ($pageNum - 1) * $rowsPerPage;
$rows = array();
$i = 0;
while(($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC, 
 SQLSRV_SCROLL_ABSOLUTE,         $offset + $i)) &amp;&amp; $i &lt; $rowsPerPage)
{
array_push($rows, $row);
$i++;
}
return $rows;
}

// Set the number of rows to be returned on a page.
$rowsPerPage = 30;

// Connect to the server.
$serverName = 'test';
$connOptions = array("Database"=&gt;"test");
$conn = sqlsrv_connect($serverName, $connOptions);
if (!$conn)
die( print_r( sqlsrv_errors(), true));

//  Define and execute the query.  
// Note that the query is executed with a "scrollable" cursor.
$sql = "select * from info";

$stmt = sqlsrv_query($conn, $sql, array(), array( "Scrollable" =&gt; 'static' ));
if ( !$stmt )
die( print_r( sqlsrv_errors(), true));

// Display the selected page of data.
echo "&lt;table border='1px' align='center'&gt;";
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);

foreach($page as $row)
echo "&lt;tr&gt;&lt;td&gt;$row[0]&lt;/td&gt;&lt;td&gt;$row[1]&lt;/td&gt;&lt;td&gt;$row[2]&lt;/td&gt;&lt;td&gt;$row[3]&lt;/td&gt;&lt;/tr&gt;";

echo "&lt;/table&gt;";
?&gt;
&lt;table align='center'&gt;
&lt;?php
// Get the total number of rows returned by the query.
// Display links to "pages" of rows.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{     
// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
for($i = 1; $i&lt;=$numOfPages; $i++)
{
 $pageLink = "?pageNum=$i";
 print("&lt;a href=$pageLink&gt;$i&lt;/a&gt;&amp;nbsp;&amp;nbsp;");
}
echo "&lt;br/&gt;&lt;br/&gt;";
}   



 sqlsrv_close( $conn );
Do you just mean the links at the bottom of your page? If so you have this code:

// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
for($i = 1; $i&lt;=$numOfPages; $i++)
{
    $pageLink = "?pageNum=$i";
    print("&lt;a href=$pageLink&gt;$i&lt;/a&gt;&amp;nbsp;&amp;nbsp;");
}


Modify this to render just  the first three links, then your ellipses (...) separator and then the final page number.

EDIT to add pseudo code
This is messy but hopefully gives you the idea, create your function, make it work and then post a new question asking how to make it prettier (for example tidy up the ifs, add logic to only render a single set of ellipses, don't repeat any rendering code).

$numOfPages = ceil($rowsReturned/$rowsPerPage);
$renderedEllipses = false;
for($i = 1; $i&lt;=$numOfPages; $i++)
{
    //If you always want to render pages 1 - 3
    if($i &lt; 4) {
        //render link
    }

    //If you always want to render current page number
    else if($i == $pageNum) {
        //render link
        //reset ellipses
        $renderedEllipses = false;
    }

    //if you always want the last page number
    else if ($i == $numOfPages - 1) {
        //render link
    }

    //make sure you only do this once per ellipses group
    else {
      if (!$renderedEllipses){
          print("...");
          $renderedEllipses = true;
      }
    }
}


EDIT added ellipses test
The $renderedEllipses flag will stop hundreds of ellipses being rendered.