Cirshiss Cirshiss - 2 years ago 56
PHP Question

Pagination dupliacates pages on first and last page

If I'm on page 2 (?p=2) This is how the pager look like

Första « 1 2 3 » Sista

However if I'm on page 1 or 3 it looks like this

(?p=1)
1 2 3 3 » Sista


(?p=3)
Första « 1 1 2 3


What am I doing wrong?
I'm using the logic from another question that was posted on this site but noone seems to experience this problem as I do. 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'>&laquo;</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'>&raquo;</span>
</a>
</li>
<li><a href='?p=". $paging_info['pages'] ."' title='Sida ". $paging_info['pages'] ."'>Sista</a></li>";

}

$out[] = "</ul></nav>";
return implode('',$out);

}

Answer Source

I think your problem is due to this two conditions before and after your for loop :

First:

if($paging_info['curr_page'] >= $max)
{
  $out[] = "<li><a href='?p=1' title='Sida 1'>1</a></li>";
}

Second:

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>";
}

You don't need this conditions because the corresponding pages are already displayed with the for loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download