喜的朋友,所以我得到了它有選項要麼選擇一個值或選擇該搜索條件的所有東西,但任何方式這裏的問題是我不知道如何得到的結果保持不變,當分頁發生?我怎麼能得到搜索結果與分頁工作?
截至目前,如果點擊下一頁或任何頁面,它將顯示空白頁面,因爲除了頁碼之外沒有任何內容被傳遞。
這裏是我的代碼
<?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>';
}
?>
我真的會很感激你爲你用這種幫助。歡呼聲
你應該通過你的GET變量到上一頁和下一頁鏈接開始。現在,您只傳遞頁碼,並且缺少說明,標題和名稱變量。 – 1sloc
@JethroVanThuyne你是這個意思嗎? – rodrix