2016-07-10 139 views
0

喜的朋友,所以我得到了它有選項要麼選擇一個值或選擇該搜索條件的所有東西,但任何方式這裏的問題是我不知道如何得到的結果保持不變,當分頁發生?我怎麼能得到搜索結果與分頁工作?

截至目前,如果點擊下一頁或任何頁面,它將顯示空白頁面,因爲除了頁碼之外沒有任何內容被傳遞。

這裏是我的代碼

<?php 

    $title = clean($_REQUEST['title']); 
    $name = clean($_REQUEST['name']); 
    $description = clean($_REQUEST['description']); 

    $criteria = array(); 

      if($title !='') 
      { 
       $criteria[] = "title = '".$title."'"; 
      }elseif($title =='All') 
       criteria[] = "title = ''"; 
      } 

      if($name !='') 
      { 
       $criteria[] = "name = '".$name."'"; 
      }elseif($name =='All') 
       criteria[] = "name = ''"; 
      } 

      if($description !='') 
      { 
       $criteria[] = "description = '".$description."'"; 
      }elseif($description =='All') 
       criteria[] = "description = ''"; 
    } 

    try { 

    $total = $db->query('SELECT COUNT(*) FROM table WHERE '.implode(' AND ', $criteria).'')->fetchColumn(); 
    global $per_page; 

    $pages = ceil($total/$per_page); 

    $page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
     'options' => array(
      'default' => 1, 
      'min_range' => 1, 
     ), 
    ))); 


    $offset = ($page - 1) * $per_page; 
    $start = $offset + 1; 
    $end = min(($offset + $per_page), $total); 


    $stmt = $db->prepare('SELECT * FROM table WHERE '.implode(' AND ', $criteria).' ORDER BY id DESC LIMIT :per_page OFFSET :offset'); 

    $stmt->bindParam(':per_page', $per_page, PDO::PARAM_INT); 
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT); 
    $stmt->execute(); 

    if ($stmt->rowCount() > 0) { 
     $stmt->setFetchMode(PDO::FETCH_ASSOC); 
     $iterator = new IteratorIterator($stmt); 
     foreach ($iterator as $row) {   

       echo $row['id']; 
     } 

    echo '<div id="pagination"> 
    <div id="pagiCount">'; 
     $prevlink = ($page > 1) ? '<span id="prev"><a href="?page=1" title="First page">First</a></span> <span id="prev"><a href="?page=' . ($page - 1) . '" title="Previous page"><<</a></span>' : ''; 
     $nextlink = ($page < $pages) ? '<span id="next"><a href="?page=' . ($page + 1) . '" title="Next page">>></a></span> <span id="next"><a href="?page=' . $pages . '" title="Last page">Last</a></span>' : ''; 
     echo '<div id="paging"><p><small>', $prevlink, ' Page ', $page, ' of ', $pages, '', $nextlink, '</small></p></div>';   
    echo '</div></div>'; 
    } else { 
     echo '<p>Nothing found.</p>'; 
    } 
} catch (Exception $e) { 
    echo '<p>Nothing found.</p>'; 
} 
?> 

我真的會很感激你爲你用這種幫助。歡呼聲

+0

你應該通過你的GET變量到上一頁和下一頁鏈接開始。現在,您只傳遞頁碼,並且缺少說明,標題和名稱變量。 – 1sloc

+0

@JethroVanThuyne你是這個意思嗎? rodrix

回答

0

你必須regenarate查詢字符串都只用一個頁面相同的參數。

所以我認爲這樣的事情會工作:

//regenerate query string with new page 
$cur_params = $_GET; 
unset($cur_params["page"]); 
$cur_url = strtok($_SERVER["REQUEST_URI"],"?") . "?" . http_build_query($cur_params, "", "&"); 
$page_back_url = $cur_url . (empty($cur_params) ? "" : "&") . "page=" . ($page - 1); 
$page_next_url = $cur_url . (empty($cur_params) ? "" : "&") . "page=" . ($page + 1); 
$page_last_url = $cur_url . (empty($cur_params) ? "" : "&") . "page=$pages";  
+0

bro但是會發生什麼?和最後一頁的選項? – rodrix

+0

追加我的鏈接到這些鏈接的錨。 –

+0

非常感謝你。 – rodrix