Jump to content

Need some help with passing the orignal search terms in a pagination script!


co.ador

Recommended Posts

    * For pagination to work, I need pass on the original search terms to the next page. I think http_build_query could be used and append this to the page links.

    * Some another person opinion said that There is no need to count the number of stores in the script I have, I  need to count the number that match the search terms. This is why there are more links that required.

 

And that's a good an idea on what could be happening.

 

Thanks

Link to comment
Share on other sites

OK, you decided to respond to me through IM for some reason, but I'll respond here.

 

You have not provided any code, so I can't give specifics, but I'll give you the basics.

 

You apparently already have some code that determines whether the user has submitted a search query and filters the results for the pagination accordingly. When you take that data to run your query you want to also save the data to session variables. But, you will also need to clear any existng variables when the user changes the query. Here is an example:

 

session_start();
$whereClauseAry = array();
$recordsPerPage = 10;
$page = 1;

if (isset($_POST))
{
    //User has submitted new criteria
    //Reset the saved criteria fields
    $_SESSION['restaurant_query'] = array();
  
    $name = trim($_POST['name']);
    $zip  = trim($_POST['zip']);
  
    if(!empty($name))
    {
        $whereClauseAry[] = "`name` = '" . mysql_real_escape_string($name) . "'";
        $_SESSION['restaurant_query']['name'] = $name;
    }
    if(!empty($zip))
    {
        $whereClauseAry[] = "`zip` = '" . mysql_real_escape_string($zip) . "'";
        $_SESSION['restaurant_query']['zip'] = $zip;
    }
}
elseif (isset($_GET['page']))
{
    //User has selected a new page for the current criteria
    //Get the saved criteria values
    if (isset($_SESSION['name']))
    {
        $whereClauseAry[] = "`name` = '" . mysql_real_escape_string($_SESSION['name']) . "'";
    }
    if (isset($_SESSION['zip']))
    {
        $whereClauseAry[] = "`zip` = '" . mysql_real_escape_string($_SESSION['zip']) . "'";
    }
    $page = (int) $_GET['page'];
}
  
$whereClauseStr = '';
if (count($whereClauseAry)>0)
{
    $whereClauseStr = "WHERE " . implode(' AND ', $whereClauseAry);
}
  
//Run the query for the current page of the current criteria
$limitStart = ($page-1) * $recordsPerPage;
$query = "SELECT * FROM restaurants {$whereClauseStr} LIMIT {$limitStart}, {$recordsPerPage}";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.