CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2014
    Posts
    3

    How to build correct URL when searching, sorting with pagination all are used.

    In my website, I am doing searching, sorting and pagination in combination for categories. The searching, sorting and pagination are all working fine separately. The searching is also working fine with pagination. But issue is regarding sorting. The sorting is not working correctly in combination with pagination and searching. Here are the following issues I am facing -


    When sorting is done first and then pagination is used then sorting parameters are lost.

    When pagination is done first and then sorting is used then sorting works but results are shown from very first page i.e. pagination parameter is lost.

    Above two issues also persist for combination of sorting and searching.

    The code also doesn't work for combination of these three features viz sorting, searching and pagination.

    I want to preserve all the URL parameters in the query string so that these three features should work in any combination. If I try to preserve the query string then on next time the query string are repeated and append to existing parameters.

    I am writing my necessary code to understand the issues; irrelevant code has been removed.

    Here is my sorting code (the parameters are supplied to sql query in a class) -


    PHP Code:
    // sorting
    $orderBy = isset($_GET['orderBy']) ? $users_obj->filter_data($_GET['orderBy']) : "";
    $sort = isset($_GET['sort']) ? $users_obj->filter_data($_GET['sort']) : "";

    if(!empty(
    $orderBy) && !empty($sort))
    {
        switch(
    $orderBy)
        {
            case 
    'category_name':
            
    $order = array("`category_name` $sort""`category_order`");
            break;
            case 
    'post':
            
    $order = array("`post` $sort""`category_order`");
            break;
            default:
            
    $order = array("`category_name`""`category_order`");
            break;
        }
    }
    else
    {
        
    $order = array("`category_name`""`category_order`");
    }

    // sort order - by default results are already sorted in ascending order so on first click you will sort by desc
    $sort = ($sort == "desc") ? "asc" "desc";

    // sort direction arrow - by default show ascending image, since results are already sorted in ascending order
    $sort_arrow = ($sort == "desc" "images/asc.png" "images/desc.png"); 
    Here is how I am building my URL with parameters for pagination when searching is done and for default stage when no searching is used:

    PHP Code:
    // $page_url and $page_suffix are being passed to pagination class
    // if search request comes
    if(isset($_GET['btnsearch']))
    {
        
    // current page url with query string
        
    $page_url $_SERVER['REQUEST_URI'];
        
    // if pagination request comes
        
    if(isset($_GET['page']))
        {
            
    $page_url substr($page_url0strpos($page_url"&page"));
        }
        
    $page_suffix "&page";
    }
    else
    {
        
    $page_url $_SERVER['PHP_SELF'];
        
    $page_suffix "?page";

    Here is my HTML code which is table header column used to do sorting. Here I am used static sorting columns with dynamic sort order i.e. asc or desc.

    PHP Code:
     <th><a href="?orderBy=category_name&sort=<?php echo $sort?>">Name</a> <span class="arrow_dir"><img src="<?php echo $sort_arrow?>" height="12" width="12" border="0" alt="arrow" /></span></th>
    I think here we need to make it dynamic so that search and pagination parameters don't get lost when sorting is done. But I don't know how? If I try to do it by using $_SERVER['REQUEST_URI'] then previous query string parameters are getting added to new one and making long wrong query string. So how can I do it in correct way. Please help me this. Thanks in advance.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: How to build correct URL when searching, sorting with pagination all are used.

    Why don't you just use a session to store previous parameters?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Mar 2014
    Posts
    3

    Re: How to build correct URL when searching, sorting with pagination all are used.

    How to do it with session? And if suppose it needs to be done without session then how we can achieve?

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: How to build correct URL when searching, sorting with pagination all are used.

    Quote Originally Posted by sachinsharma1507 View Post
    How to do it with session?
    http://php.net/manual/en/function.session-start.php

    Quote Originally Posted by sachinsharma1507 View Post
    And if suppose it needs to be done without session then how we can achieve?
    Use hidden <inputs> to store the data.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured