Good day. There is a code that displays pagination on the site. But the fact is that it displays all links. For example, if I have 1000 entries in the database, the script will display 500 links per page (if there are 2 entries per page). Tell me, please, how to make it so that pagination displays pages 5, for example.
if (isset($_GET['page'])) { $CUR_PAGE=($_GET['page']); } else { $CUR_PAGE=1; } $res=mysql_query("SELECT count(*) FROM articles"); $row=mysql_fetch_row($res); $total_rows=$row[0]; $ITEM_PER_PAGE = 2; $start=abs(($CUR_PAGE-1)*$ITEM_PER_PAGE); $sql=mysql_query("SELECT * FROM articles ORDER BY id DESC LIMIT $start,$ITEM_PER_PAGE"); require_once 'tpl/index.php'; $uri=strtok($_SERVER['REQUEST_URI'],"?")."?"; if (isset($_GET['page'])) unset($_GET['page']); if (count($_GET)) { foreach ($_GET as $k => $v) { if ($k != "page") $uri.=urlencode($k)."=".urlencode($v)."&"; } } $num_pages=ceil($total_rows/$ITEM_PER_PAGE); for($i=1;$i<=$num_pages;$i++) $PAGES[$i]=$uri.'page='.$i; if ($CUR_PAGE <=1) { echo '<span>Prev</span>'; } else { $j = $CUR_PAGE - 1; echo '<a href="?page='.$j.'">Prev</a>'; } foreach ($PAGES as $i => $link) { if ($i == $CUR_PAGE) { echo '<b>'.$i.'</b>'; } else { echo '<a href="'.$link.'">'.$i.'</a>'; } } if ($CUR_PAGE == $num_pages) { echo '<span>Next</span>'; } else { $j = $CUR_PAGE + 1; echo '<a href="?page='.$j.'">Next</a>'; } if ($CUR_PAGE == $num_pages) { echo '<span>Last</span>'; } else { echo '<a href="?page='.$num_pages.'">Last</a>'; }