Say i have a list of items on a page . Now this list can be filtered by price ( in a simple POST form ) OR by discount (in another form element) OR by category links in the sidebar.
Now if the user first filters by price and then clicks on a category, the first setting of filtering by price is lost! Whats the best way to have the old input stay ? So that the list of items is filtered both by category and price? I guess I can use sessions for this. But is there a better way of doing this in an MVC architecture (codeigniter) .
In the past I came up with code that looks like this :

echo " <a " ;    //buiding category list
echo "href='" ;
if ( isset($_GET['colony']))              // if getting items from colony or walking distance
{
    $colony = htmlspecialchars($_GET['colony']) ;
    echo "show_delhi_items.php?colony={$colony}&category_id={$categories_row['id']}" ;
}

else                                    // if getting all items
    echo "show_delhi_items.php?category_id={$categories_row['id']} " ;

echo "'>{$categories_row['category']}</a></span>            <br/>";
}

If anyone understood what I am trying to say , do guide me please

Thanks
Edit: The onyl way I can think of doing this is to have hidden form elements . Would that be a wise approach ?

Recommended Answers

All 2 Replies

Member Avatar for diafol

If you need to maintain state between pages you need to use one of the superglobals ($_POST, $_GET, $_COOKIE or $_SESSION). (or even localStorage for client-side stuff - not relevant here unless you use ajax) Your choice which one you go for, hidden form elements are prob. easiest. Provided you don't include any secret data!

I am going search filter via GET parameters.
Below is the code. Try this:

<?
function getCurrPageUrl($unset=array())
{
    $currPageName = basename($_SERVER['PHP_SELF']);
    $arr = $_GET;
    foreach($unset as $one)
    {
        unset($arr[$one]);
    }       
    return $url = $currPageName.'?'.http_build_query($arr);
}
?>
<a href="<?=getCurrPageUrl(array('category'));?>&category=mobile">Category</a>
<a href="<?=getCurrPageUrl(array('price'));?>&price=100">Price</a>
<a href="<?=getCurrPageUrl(array('color'));?>&color=black">Color-black</a>
<a href="<?=getCurrPageUrl(array('color'));?>&color=white">Color-white</a>

Each time you click on any search parameter page's url is changed accordingly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.