2017-08-16 78 views
2

如果我是第2頁(P = 2),這是尋呼機看怎麼樣 Första « 1 2 3 » Sista 但是,如果我第1或3看起來這分頁dupliacates第一頁和最後一頁頁上

(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'>&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); 

} 

回答

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循環顯示。

+0

謝謝!現在按預期工作 – Cirshiss

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'>&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); 

    } 

    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'>&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); 

} 
相關問題