2013-08-26 153 views
-1

我在第一頁上有一個搜索欄,結果在第二頁上用分頁顯示。問題是分頁不符合$_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>"; ?> 

回答

2

更改您的代碼是這樣的......

<form action="products.php" method="get"> 
    <input type="text" name="search"> 
    <input type="Submit"> 
    </form> 

......還有......

$options = array(
       'results_per_page'    => 100, 
       'url'       => 'products.php?search=' . urlencode($_GET['search']) . '&page=*VAR*', 
       'db_handle'      => $dbh 
      ); 
+0

感謝兄弟修復了這個問題。 – amdvb

-1

最簡單的解決方案可能會在您的表單中使用隱藏字段:

<form action="products.php" method="post"> 
    <input type="text" name="search"> 
    <input type="hidden" name="page" value="<?php echo $_GET['page']; ?>"> 
    <input type="Submit"> 
</form> 
+0

他需要他的分頁代碼中的搜索值而不是搜索框。 – cmorrissey

-1

嘗試Session變量,他們讓你保存它,直到瀏覽器被關閉(或切換到另一個網站,也許?),並將其存儲跨頁。

http://www.tizag.com/phpT/phpsessions.php

另一種方法是餅乾,這將讓你存儲只是,只要你想,除非用戶刪除cookie。

http://www.tizag.com/phpT/phpcookies.php

+2

不要這樣做,如果你在一個搜索的第4頁,然後你做另一個搜索...現在你再次在第4頁 – cmorrissey

+0

據我瞭解,他想存儲的所有值是搜索的值,而不是頁碼。總是將它作爲一個GET變量而不是有時候GET,有時候可能會更容易,但是你絕對可以將它存儲在一個會話變量中。基本上,如果有一個GET變量使用它並存儲爲會話變量。否則,請查找會話變量並使用它。 – hughmanwho