[英]How to limit pagination function navigation to 10 per page
我有一个用于我的数据库搜索的分页功能,该功能将每页的结果限制为25个。但是,我大约有2300个条目,当有人进行查询很多结果的搜索时,我最终得到90个左右的分页链接我页面的底部。 我想限制分页导航器一次只能显示10页,并根据页面拼写进行相应调整。
我不太确定如何调整脚本。
任何帮助将不胜感激。
我当前的功能是这样的:
$ search_function是用于获取正确网址的javascript函数,$ classical_guitar是指图像。
function generate_page_links($cur_page, $num_pages) {
global $search_function, $classical_guitarL, $classical_guitarR;
$page_links = '';
// If this page is not the first page, generate the "previous" link
if ($cur_page > 1) {
$page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page - 1) . "');\">" . $classical_guitarL . "</a> ";
}
else {
$page_links .= '';
}
// Loop through the pages generating the page number links
for ($i = 1; $i <= $num_pages; $i++) {
if ($cur_page == $i) {
$page_links .= ' ' . $i;
}
else {
$page_links .= '<a href="javascript:' . $search_function . "('', '" . $i . "');\"> " . $i . "</a> ";
}
}
// If this page is not the last page, generate the "next" link
if ($cur_page < $num_pages) {
$page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page + 1) . "');\">" . $classical_guitarR . "</a> ";
}
else {
$page_links .= '';
}
return $page_links;
}
在这里,我修改了您的功能:
<?php
function generate_page_links($cur_page, $num_pages)
{
global $search_function, $classical_guitarL, $classical_guitarR;
$page_links = '';
// If this page is not the first page, generate the "previous" link
if ($cur_page > 1)
{
$page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page - 1) . "');\">" . $classical_guitarL . "</a> ";
}
else
{
$page_links .= '';
}
$pager_num = 7; // How many page number you wish to display to the left and right sides of the current page
$index_start = ($cur_page - $pager_num) <= 0 ? 1 : $cur_page - $pager_num;
$index_finish = ($cur_page + $pager_num) >= $num_pages ? $num_pages : $cur_page + $pager_num;
if (($cur_page - $pager_num) > 1) { $page_links .= '...'; } // Display ... when there are more page items than $page_num
// Loop through the pages generating the page number links
// NOTE: I've modified the for index pointers here...
for ($i = $index_start; $i <= $index_finish; $i++)
{
if ($cur_page == $i)
{
$page_links .= ' ' . $i;
}
else
{
$page_links .= '<a href="javascript:' . $search_function . "('', '" . $i . "');\"> " . $i . "</a> ";
}
}
if (($cur_page + $pager_num) < $num_pages) { $page_links .= '...'; } // Display ... when there are more page items than $page_num
// If this page is not the last page, generate the "next" link
if ($cur_page < $num_pages)
{
$page_links .= '<a href="javascript:' . $search_function . "('', '" . ($cur_page + 1) . "');\">" . $classical_guitarR . "</a> ";
}
else
{
$page_links .= '';
}
return $page_links;
}
?>
希望对您有所帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.