2013-08-26 92 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