如果我在第2页(?p = 2)这就是寻呼机的样子
Första « 1 2 3 » Sista
但是,如果我在第1页或第3页,它看起来像这样
(?p = 1)1 2 3 3 » Sista
(?p = 3)Första « 1 1 2 3
我做错了什么? 我正在使用本网站上发布的另一个问题的逻辑,但似乎没有人像我一样遇到这个问题。 Limit pagination page number
function get_paging_info($tot_rows,$pp,$curr_page)
{
$pages = ceil($tot_rows / $pp); // calc pages
$data = array(); // start out array
$data['si'] = ($curr_page * $pp) - $pp; // what row to start at
$data['pages'] = $pages; // add the pages
$data['curr_page'] = $curr_page; // Whats the current page
return $data; //return the paging data
}
function pagin($db,$sql,$limit,$curr_page,$max_pages=7)
{
$res = $db->query($sql);
$count = $res->num_rows;
$paging_info = get_paging_info($count,$limit,$curr_page);
$out[] = "
<nav aria-label='Page navigation'>
<ul class='pagination'>";
if($paging_info['curr_page'] > 1)
{
$out[] = "
<li><a href='?p=1' title='Sida 1'>Första</a></li>
<li>
<a href='?p=". ($paging_info['curr_page'] - 1) ."' aria-label='Föregående'>
<span aria-hidden='true'>«</span>
</a>
</li>";
}
$max = $max_pages;
if($paging_info['curr_page'] < $max)
{
$sp = 1;
}
elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
{
$sp = $paging_info['pages'] - $max + 1;
}
elseif($paging_info['curr_page'] >= $max)
{
$sp = $paging_info['curr_page'] - floor($max/2);
}
if($paging_info['curr_page'] >= $max)
{
$out[] = "<li><a href='?p=1' title='Sida 1'>1</a></li>";
}
for($i = $sp; $i <= ($sp + $max -1);$i++)
{
if($i > $paging_info['pages']) continue;
if($paging_info['curr_page'] == $i)
{
$out[] = "<li class='active'><span class='strong'>". $i ."</span></li>";
}
else
{
$out[] = "<li><a href='?p=". $i ."' title='Sida ". $i ."'>". $i ."</a></li>";
}
}
if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2)))
{
$out[] = "<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>". $paging_info['pages'] ."</a></li>";
}
if($paging_info['curr_page'] < $paging_info['pages'])
{
$out[] = "
<li>
<a href='?p=". ($paging_info['curr_page'] + 1) ."' aria-label='Nästa' title='Page ". ($paging_info['curr_page'] + 1) ."'>
<span aria-hidden='true'>»</span>
</a>
</li>
<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>Sista</a></li>";
}
$out[] = "</ul></nav>";
return implode('',$out);
}
答案 0 :(得分:2)
我认为您的问题是由于for
循环之前和之后的这两个条件:
<强>首先强>
if($paging_info['curr_page'] >= $max)
{
$out[] = "<li><a href='?p=1' title='Sida 1'>1</a></li>";
}
<强>第二强>
if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max / 2)))
{
$out[] = "<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>". $paging_info['pages'] ."</a></li>";
}
您不需要此条件,因为相应的页面已经显示for
循环。
答案 1 :(得分:0)
试试这个。我已从您的函数中删除了一些额外的代码。用以下代码替换pagin
函数,然后尝试
function pagin($db,$sql,$limit,$curr_page,$max_pages=7)
{
$res = $db->query($sql);
$count = $res->num_rows;
$paging_info = get_paging_info($count,$limit,$curr_page);
$out[] = "
<nav aria-label='Page navigation'>
<ul class='pagination'>";
if($paging_info['curr_page'] > 1)
{
$out[] = "
<li><a href='?p=1' title='Sida 1'>Första</a></li>
<li>
<a href='?p=". ($paging_info['curr_page'] - 1) ."' aria-label='Föregående'>
<span aria-hidden='true'>«</span>
</a>
</li>";
}
$max = $max_pages;
if($paging_info['curr_page'] < $max)
{
$sp = 1;
}
elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max / 2)) )
{
$sp = $paging_info['pages'] - $max + 1;
}
elseif($paging_info['curr_page'] >= $max)
{
$sp = $paging_info['curr_page'] - floor($max/2);
}
for($i = $sp; $i <= ($sp + $max -1);$i++)
{
if($i > $paging_info['pages']) continue;
if($paging_info['curr_page'] == $i)
{
$out[] = "<li class='active'><span class='strong'>". $i ."</span></li>";
}
else
{
$out[] = "<li><a href='?p=". $i ."' title='Sida ". $i ."'>". $i ."</a></li>";
}
}
if($paging_info['curr_page'] < $paging_info['pages'])
{
$out[] = "
<li>
<a href='?p=". ($paging_info['curr_page'] + 1) ."' aria-label='Nästa' title='Page ". ($paging_info['curr_page'] + 1) ."'>
<span aria-hidden='true'>»</span>
</a>
</li>
<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>Sista</a></li>";
}
$out[] = "</ul></nav>";
return implode('',$out);
}