-1

I have a search bar on the first page and the results are shown on the second page with pagination. The problem is the pagination isn't working with $_GET or $_POST variable.

what I meant is like when the form is submitted the url on second page changes to something like

products.php?search=something and I am able to see the results that show up.

However when I hit the next button of the pagination I get undefined index search error and the url changes to products.php?page=2

so is there any way that I can store the $_GET['search']; value so that I can use it when the page number changes?

  <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>"; ?>
4

3 回答 3

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
            );
于 2013-08-26T15:56:22.830 回答
-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>
于 2013-08-26T15:56:57.443 回答
-1

试试 Session 变量,它们让你存储它直到浏览器关闭(或者你切换到另一个网站?)并跨页面存储它。

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

另一种选择是 cookie,除非用户删除他们的 cookie,否则它可以让您存储任意时间。

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

于 2013-08-26T15:58:30.590 回答