我在第一頁上有一個搜索欄,結果在第二頁上用分頁顯示。問題是分頁不符合$_GET
或$_POST
變量。存儲GET變量的值
我的意思是,當表單提交的第二頁的URL改變爲類似
products.php?search=something
一樣,我能夠看到顯示出來的結果。
然而,當我打的分頁我得到undefined index search error
的下一個按鈕和URL變化products.php?page=2
所以是有,我可以存儲$_GET['search'];
值,這樣我可以用它的頁號更改時,任何方式?
<form action="products.php" method="post">
<input type="text" name="search">
<input type="Submit">
</form>
products.php
<?php
/*
* Connect to the database (Replacing the XXXXXX's with the correct details)
*/
try
{
$dbh = new PDO('mysql:host=localhost;dbname=db', 'root', '');
}
catch(PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
/*
* Get and/or set the page number we are on
*/
if(isset($_GET['page']))
{
$page = $_GET['page'];
}
else
{
$page = 1;
}
/*
* Set a few of the basic options for the class, replacing the URL with your own of course
*/
$options = array(
'results_per_page' => 100,
'url' => 'products.php?page=*VAR*',
'db_handle' => $dbh
);
$q = $_GET['search'];
/*
* Create the pagination object
*/
try
{
$paginate = new pagination($page, "SELECT * FROM products where title LIKE '%$q%' order by id desc", $options);}
catch(paginationException $e)
{
echo $e;
exit();
}
if($paginate->success == true)
{
$paginate_result = $paginate->resultset->fetchAll();
foreach($paginate_result as $row)
{
echo $row['title'];
}
?>
<?php echo "<li>"; //this is next and prev button for the pagination class if results exceed the limit.
echo $ball->links_html;
echo "</li>"; ?>
感謝兄弟修復了這個問題。 – amdvb