Jump to content

Storing product page view settings for specific category


Recommended Posts

I need a way to store the settings for a specific category that user is viewing.  Say they click into category "A", and choose Sort By: Name and Products per Page: 24 and then click into category "C", and choose Sort By: Price and Products per Page: 48. Then click back into category "A", I want their initial settings choices of Sort By: Name and Products per Page: 24 to come back up. I also want to keep this in the URL as best as possible so they can bookmark it. How do I do this?

 

if(isset($_GET['cat'])){
$cat=$_GET['cat'];
$cat=str_replace("-"," ", $cat);

session_start();
if(isset($_GET['num_products'])){
$num_products_per_page=$_GET['num_products'];
$num_products_per_page = stripslashes($num_products_per_page);
$num_products_per_page = mysql_real_escape_string($num_products_per_page);
}
else{
$num_products_per_page="20";
}
$num_products='';

if(isset($_GET['sort_by'])){
$sort_by_selected=$_GET['sort_by'];
$sort_by_selected = stripslashes($sort_by_selected);
$sort_by_selected = mysql_real_escape_string($sort_by_selected);
}
else{
$sort_by_selected="product_id";
}
$sort_by='';
}

Link to comment
Share on other sites

If you want the options to be remembered you will need to store them somewhere. The most logical options would be in cookies or in session data. You can use cookies to have the options persists across sessions or use the session variable to just remember them for the session. You can then use the cookie/session value to implement the appropriate option on each page load. It is an easier solution since you don't have to modify all the links on your pages.

 

So, for the first part to remember the options. When the user selects an option you will detect in on the next page load. Just store that option in the session/cookie as a sub-element of a category element. So, in your example, if the user selected "category "A", and choose Sort By: Name and Products per Page: 24", then you would store the options like this:

if(isset($_GET['num_products'])){
$num_products_per_page=$_GET['num_products'];
$cookie[$cat]['num_products_per_page'] = $num_products_per_page;

//.....

if(isset($_GET['sort_by'])){
$sort_by_selected=$_GET['sort_by'];
$cookie[$cat]['sort_by_selected'] = $sort_by_selected;

 

Now, if you really want to include the options int eh URLs then you will need to add some logic anywhere that you provide a link to another page where you want the options "remembered". I would suggest creating a function to create the URLs. For example, you create a function that takes the base URL and the category. The function then uses the category to see if there are remembered options for that category and append  them to the base URL.

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.