Php pagination numbered page links

paginationphp

This is a pagination code used for the navigation, any ideas how to get this code to display simply a numbered list of the pages as links?

if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} 
else {
   $pageno = 1;
}
if(isset($_GET['niche']))
{


$query = "SELECT count(*) FROM studies WHERE niche = '{$_GET['niche']}'";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);
}


$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
$rows_per_page = 4;
$lastpage      = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
}
if ($pageno < 1) {
   $pageno = 1;
} // if
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM studies WHERE niche = '{$_GET['niche']}' $limit";
$result = mysql_query($query, $connection) or trigger_error("SQL", E_USER_ERROR);

and…

if ($pageno == 1) {
   echo "<div class='container'>FIRST PREV ";
} else {
   echo "<div class='container'> <a href='{$_SERVER['PHP_SELF']}?pageno=1&niche={$_GET['niche']}'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage&niche={$_GET['niche']}'>PREV</a> ";
} // if
echo " ( Page $pageno of $lastpage ) ";
if ($pageno == $lastpage) {
   echo " NEXT LAST</div><br />";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage&niche={$_GET['niche']}'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage&niche={$_GET['niche']}'>LAST</a></div><br /> ";
} // if
?>

Best Solution

Try this:

$totalpages = ceil($numrows / $rows_per_page);

if($totalpages >= 1){ $pagelinkcount = 1; } else { $pagelinkcount = 0; }

while($pagelinkcount <= $totalpages && $totalpages > 1) {

     echo "<a href=\"/page/{$pagelinkcount}\">{$pagelinkcount}</a>&nbsp;";

     $pagelinkcount++;

}

On a side note, as Ian Elliot pointed out in the comments for your question, using $_GET in an SQL query leaves your database VERY vulnerable, and is thus considered an extremely insecure coding practice. You should escape and parse the $_GET data that you need diligently before passing it to the DB.