2017-08-03 25 views
0

我使分頁功能,但結果仍然不正確。我想要顯示的頁面只有8個,但顯示的結果爲9個。所以當我點擊數字9時什麼都不顯示。這是我的分頁碼:在codeigniter中使手動分頁

<?php if($current_page>1){ ?>     
     <nav class="pagination pagination-blue"> 
        <a class="ajaxPage" href="<?=base_url()?>tracking/searchTiket?page=<?=($current_page-1)?>&&no_ticket=<?=$no_ticket?>&&s_month=<?=$month?>&&s_year=<?=$year?>">prev</a> 
        </nav> 
        <?php } ?> 
       <?php 
       $max = 5; 
       if($current_page < $max) 
        $sp = 1; 
       elseif ($current_page >= ($page_size - floor($max/2))) 
        $sp = $page_size - $max + 1; 
       elseif($current_page >= $max) 
        $sp = $current_page - floor($max/2); 
        for($i = $sp; $i<=($sp + $max -1); $i++){ 
         if($i > $page_size) 
          continue; 
         if($current_page == $i) 
       ?>   
     <nav class="pagination pagination-blue"> 
      <a class="ajaxPage" href="<?=base_url()?>tracking/searchTiket?page=<?=$i?>&&no_ticket=<?=$no_ticket?>&&s_month=<?=$month?>&&s_year=<?=$year?>"><?=floor($i)?></a></nav> 
       <?php 
        } 
       ?> 
       <?php if(ceil($page_size)!=($current_page+1)){ ?> 
     <nav class="pagination pagination-blue"> 
       <a class="ajaxPage" href="<?=base_url()?>tracking/searchTiket?page=<?=($current_page+1)?>&&no_ticket=<?=$no_ticket?>&&s_month=<?=$month?>&&s_year=<?=$year?>">next</a> </nav> 
       <?php } ?>   

回答

0

我禁止發表評論。 我只想給你一個主意。 嘗試先檢查您的查詢,如果它提供了您想要檢索的正確數量的數據。

這是我最古老的分頁設計之一,很高興我保留它。

這是首頁:所有工作正常。 enter image description here

而這是最後一頁......正如你所看到的,沒有結果。 enter image description here

如果您的查詢很好,可能是您的START & LIMIT問題。

我用這個代碼,我從互聯網年前看到:(對不起,我刪除了原文評論)

$tbl_name="your_table"; 
$adjacents = 10; // (10*2)+1 = 21 pages between next and prev 

$query = "SELECT COUNT(*) as num FROM $tbl_name"; 
$total_pages = mysqli_fetch_array(mysqli_query($query)); 
$total_pages = $total_pages[num]; 

$targetpage = "same_page.php"; 
$limit = 100; // rows to be displayed per page 
$page = $_GET['page']; 
if($page) 
{ 
    $start = ($page - 1) * $limit; // ** this caused the problem ** 
    $start = ($page + $limit) - 1; // ** this fixed the problem ** 
} 
else{ 
    $start = 0; 
} 

$sql = "SELECT * FROM $tbl_name LIMIT $start, $limit "; 
$result = mysqli_query($sql); 

if ($page == 0) $page = 1; 
$prev = $page - 1; 
$Next = $page + 1; 
$lastpage = ceil($total_pages/$limit); 
$lpm1 = $lastpage - 1; 

$pagination = ""; 
if($lastpage > 1) 
{ 
    $pagination .= "<div class=\"pagination\">"; 
    //Prev button 
    if ($page > 1) 
     $pagination.= "<a href=\"$targetpage?search=$get&page=$prev\" >« Latest&nbsp</a>"; 
    else 
     $pagination.= "<span class=\"disabled\">« Latest&nbsp</span>"; 

    //pages 
    if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up 
    { 
     for ($counter = 1; $counter <= $lastpage; $counter++) 
     { 
      if ($counter == $page) 
       $pagination.= "<span class=\"current\">&nbsp$counter</span>"; 
      else 
       $pagination.= "<a href=\"$targetpage?search=$get&page=$counter\" >&nbsp$counter</a>"; // just ignore search=$get, I believe you don't need this   
     } 
    } 
    elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some 
    { 
     //close to beginning; only hide later pages 
     if($page < 1 + ($adjacents * 2)) 
     { 
      for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) 
      { 
       if ($counter == $page) 
        $pagination.= "<span class=\"current\">$counter</span>"; 
       else 
        $pagination.= "<a href=\"$targetpage?search=$get&page=$counter\" >$counter</a>";      
      } 
      $pagination.= "..."; 
      $pagination.= "<a href=\"$targetpage?search=$get&page=$lpm1\" >$lpm1</a>"; 
      $pagination.= "<a href=\"$targetpage?search=$get&page=$lastpage\" >$lastpage</a>";  
     } 
     //in middle; hide some front and some back 
     elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) 
     { 
      $pagination.= "<a href=\"$targetpage?search=$get&page=1\" >1</a>"; 
      $pagination.= "<a href=\"$targetpage?search=$get&page=2\" >2</a>"; 
      $pagination.= "..."; 
      for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) 
      { 
       if ($counter == $page) 
        $pagination.= "<span class=\"current\">$counter</span>"; 
       else 
        $pagination.= "<a href=\"$targetpage?search=$get&page=$counter\" >$counter</a>";      
      } 
      $pagination.= "..."; 
      $pagination.= "<a href=\"$targetpage?search=$get&page=$lpm1\" >$lpm1</a>"; 
      $pagination.= "<a href=\"$targetpage?search=$get&page=$lastpage\" >$lastpage</a>";  
     } 
     //close to end; only hide early pages 
     else 
     { 
      $pagination.= "<a href=\"$targetpage?search=$get&page=1\" >1</a>"; 
      $pagination.= "<a href=\"$targetpage?search=$get&page=2\" >2</a>"; 
      $pagination.= "..."; 
      for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) 
      { 
       if ($counter == $page) 
        $pagination.= "<span class=\"current\">$counter</span>"; 
       else 
        $pagination.= "<a href=\"$targetpage?search=$get&page=$counter\" >$counter</a>";   


      } 
     } 
    } 
    //Next button 
    if ($page < $counter - 1) 
     $pagination.= "<a href=\"$targetpage?search=$get&page=$Next\" >&nbspPrev »</a>"; 
    else 
     $pagination.= "<span class=\"disabled\">&nbspPrev »</span>"; 
    $pagination.= "</div>\n"; 
} 
?> 

/** 
* YOUR TABLE 
* GOES 
* HERE 
*/ 

<?php echo $pagination;?> 

結果,最後一頁顯示正確的數據 enter image description here

我不擅長CI,但我希望這有助於。祝你好運!

+0

我已經檢查了我的查詢,並沒有問題,所以我認爲我的錯在分頁邏輯@Jorz –

+0

我可以有代碼嗎?謝謝 –

+0

我更新了我的答案 – Jorz