2012-06-03 65 views
1

我剛剛學習分頁。我無法使其適用於搜索結果。它會正確顯示第一頁並顯示正確的鏈接數量,但點擊任何鏈接(即使在第1頁上)都會進入空白頁面。php分頁式搜索結果無法顯示

可有人請告訴我,我做錯了什麼:

下面的代碼是在點擊搜索按鈕時。

<?php 

include('includes/connect-db.php'); 
include('includes/functions.php'); 

if (isset($_GET['searchbtn'])){ 
    $product=$_GET['products']; 
    $status=$_GET['ticket_status']; 
    $order_by=$_GET['order_by']; 
    $ticket_type=$_GET['ticket_type']; 

    #check if product has been selected 
    if ($_GET['products'] == 'select'){ 
     echo '<font class="error">&nbsp Please select a product to search.</font>'; 
    }else{ 
     if ($status == 'All' AND $order_by == 'All' AND $ticket_type == 'All'){ 
      $page_query="SELECT * FROM tickets WHERE product='$product' ORDER BY created DESC"; 
     }elseif ($ticket_type == 'BOX'){ 
      $page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' AND pbi <> '-' ORDER BY '$order_by' "; 
     }elseif ($ticket_type == 'PVR'){ 
      $page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' AND inc <> '-' ORDER BY '$order_by' "; 
     }else{ 
      $page_query="SELECT * FROM tickets WHERE product='$product' AND status='$status' ORDER BY created DESC"; 
     } 
    }#end of else 

    $results_per_page=3; 
    $result_set=pagination($results_per_page,$page_query); 

    $query=$result_set['query']; 
    $pages=$result_set['pages']; 

    #SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset. A resource on success or False on Error 

    if (!empty($query)){ 
     $result = mysqli_query($db,$query) or die("My query ($query) generated an error: ".mysql_error()); 
     $num_results = mysqli_num_rows($result); 

     if ($num_results > 0){ 
      displayTicket($result); 
      if ($pages > 1){ 
       echo '</br>'; 
       for($i=1;$i<=$pages;$i++){ 
        echo ' <a href="?page='.$i.'">'.$i.'</a> '; 
       } 
      } 
     }else{ 
      echo "&nbsp No Records found."; 
     } 
    }#query string is not empty 
} 


?> 

我已經把分頁代碼在一個單獨的功能:

function pagination($per_page, $pages_query){ 
    include('includes/connect-db.php'); 
    $pagination[]=array(); 

    #total result count 
    $total_result=mysqli_query($db,$pages_query); 

    #ceil takes a decimal number and gives the nearest whole number 
    $total=mysqli_num_rows($total_result); 
    $pages = ceil($total/$per_page); 

    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; 
    $start = ($page - 1) * $per_page; 

    $query = " LIMIT $start,$per_page"; 
    $query = $pages_query.$query; 
    $pagination['query'] = $query; 
    $pagination['pages'] = $pages; 
    return $pagination; 
} 

注:鏈接到其他網頁分頁只有當我用搜索功能嘗試失敗。我已經在列表中列出信息的頁面上嘗試過,鏈接正常工作。

+1

同樣在你的函數中'$ pagination [] = array();'應該是'$ pagination = array();'否則它會破壞啓動它的目的。 ' –

+0

這個空白頁的網址是什麼?你能得到空白頁面發佈有關它所接收的參數的任何信息嗎? – octern

+0

@octern:當我第一次運行搜索時,我得到: http://localhost/test/searchProductForm.php?products = Box&ticket_status = All&order_by = All&ticket_type = All&searchbtn = Go%21 當我點擊頁面1或2時: http://localhost/test/searchProductForm.php?page = 1 http://localhost/test/searchProductForm.php?page = 2 – greenpool

回答

1

它在我看來像問題是鏈接中的信息:localhost/test/searchProductForm.php?page=1告訴searchProductForm哪個頁面顯示,但它不告訴它什麼是搜索信息。沒有這一點,你的搜索頁面不知道要顯示什麼。嘗試更改分頁鏈接以包含原始搜索鏈接中的所有信息(即,您的products,ticket_status,order_byticket_type參數)。

+0

謝謝。我做了以下事情,它似乎保留了我點擊頁碼時選擇的選項,但它仍然沒有顯示結果。 'echo「$i」;' 所以現在當我點擊第2頁時,例如:'http://localhost/test/searchProductForm.php?page = 2&products = Box&tic?我設置了頁面的回顯,開始和結果每頁,當我運行搜索我得到: 頁1 開始0 每頁3 但我當我點擊頁面1或者第2頁鏈接,它不會回顯。 – greenpool

+0

你可以得到它迴應任何東西到頁面?如果沒有,那麼頁面可能會死亡。你有沒有設置顯示錯誤,或者你檢查錯誤日誌?如果沒有,嘗試在頁面的開頭添加'ini_set('display_errors',1)'這一行,看看它是否告訴你什麼。 – octern

+0

謝謝。完成了,沒有任何東西。仍然向我展示一個空白頁面(我的選擇完好無損)。也許我需要建立一個簡單的搜索頁面並測試它..? – greenpool

-1

我知道這是一箇舊的線程,但對於任何人都無法解決上面使用的代碼類似的問題,它可能是在文檔開始缺少session_start()。

我這樣說,因爲雖然代碼試圖獲取由_page = & products = etc傳遞給$ _GET語句的會話變量,但它們不會從URL中檢索到變量,但需要將session_start()在PHP文檔的開始處將信息從URL傳遞到字符串。

+0

只有當你想訪問'$ _SESSION'變量時,你才需要訪問'$ _GET'變量的會話 – Bowdzone