• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

How do I fix this php search/filter form problem?

I am setting up a search page using PHP and I am having a problem with my Next / Previous page links.

You're able to filter your results by price range for example. If you click on submit, the page will only display items within your price range. This works without any problems.

You can also choose up how many items you want to display per page (by drop down box).  If you choose 10 items per page and click on submit, the first search result page will only display 10 items. If the search results are more than 10 items, a 'Next Page' link will display.

My problem is if you click on the 'Next Page' item, all the filters for the search results are removed (in this example, the price range would set back up to the default and the number of items displayed per page would go back to 5 (which is the default).

I'm not too sure what I am doing wrong or what code I should provide. Please let me know.
0
JohnnyBCJ
Asked:
JohnnyBCJ
  • 7
  • 4
  • 2
  • +1
4 Solutions
 
leakim971PluritechnicianCommented:
I understood your script work fine but you may improve it to be able to use all available filters and return the right results :
- page number
- price min
- price max
- search words
and so on...

your mysql query need to be based on all this parameters and return the appropriate set of results

each time you click on Next or Previous you send back the original query and the new page
if needed you may store the current filter composite in hidden fields or session variables
0
 
clampsCommented:
either you always transfer that data with each click by POST or GET or you start a session for every connection and store those values in the session.
GET might be the tool of your choice because users will be able to send the url with the filter settings to other users.
Cheers
0
 
user_nCommented:
http://php.net/manual/en/features.sessions.php
or form with hidden elements
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
JohnnyBCJAuthor Commented:
leakim971:
I understood your script work fine but you may improve it to be able to use all available filters and return the right results :
- page number
- price min
- price max
- search words
and so on...

your mysql query need to be based on all this parameters and return the appropriate set of results

each time you click on Next or Previous you send back the original query and the new page
if needed you may store the current filter composite in hidden fields or session variables


That's exactly my problem. When I click on the 'Next Page' link, I'm somehow managing to send back the original query back and not the latest filtered results.

Lets say the price range by default is $1 to $100. I change that to be $60 to $80. I click on submit and I get all the 19 items back. The first 5 is displayed, but instead I want to display 10 items, so I change the 'displayed items per page' to 10 (I could do this in the 1st step or two steps, same result). I click submit and now I get items 1 to 10 displayed on my screen. If I click the next item, What is suppose to display is items 11 to 19 but instead I'll get items 11 to 15, without the price filter.

When I click on the 'Next Page' link, all I'm trying to do is change what is displayed on the screen to move to the next page result with the SAME filters applied. I can't figure out what I'm doing wrong.
0
 
JohnnyBCJAuthor Commented:
clamps:
either you always transfer that data with each click by POST or GET or you start a session for every connection and store those values in the session.
GET might be the tool of your choice because users will be able to send the url with the filter settings to other users.
Cheers


I do transfer the filters by GET. I have not attempted to store these values in a session tho. Shouldn't I always be able to post back to the URL with all the variables? I guess my problem is that if the user changes a filter, I'm not able to retrieve this new value unless it gets submitted back to the form?

The Search page and the results page are the same.
0
 
user_nCommented:
You can use GET method and in the back page url you can add the filters and use them
0
 
JohnnyBCJAuthor Commented:
user_n:
You can use GET method and in the back page url you can add the filters and use them


That's what I'm attempting to do.
I have a PHP function that returns the query based on the variables/filters described. For whatever reason when I click on the next page link, the query is resetting to the filter's default values.
0
 
leakim971PluritechnicianCommented:
You need to customize your script to get each query like a new one and build a SQL query to get the right result
you may need to filter a bit with PHP but your SQL query need to be build from scratch especally with the page number
use SELECT ... FROM ... ORDER BY ...  LIMIT page_number, page_number + number_of_results_to_display

page_number
and
number_of_results_to_display are the brand new parameters/filters from the last form submission

http://dev.mysql.com/doc/refman/5.0/fr/select.html (check limit section)
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
0
 
JohnnyBCJAuthor Commented:
leakim971:
You need to customize your script to get each query like a new one and build a SQL query to get the right result
you may need to filter a bit with PHP but your SQL query need to be build from scratch especally with the page number
use SELECT ... FROM ... ORDER BY ...  LIMIT page_number, page_number + number_of_results_to_display

page_number
and
number_of_results_to_display are the brand new parameters/filters from the last form submission

http://dev.mysql.com/doc/refman/5.0/fr/select.html (check limit section)
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm


Once again, this is exactly what I am attempting to do. I guess this problem is pretty hard to figure out unless I posted some code?
0
 
leakim971PluritechnicianCommented:
Just your current mysql query
0
 
JohnnyBCJAuthor Commented:


My Query by default is:

SELECT * FROM Inventory where YEAR >=1970 and YEAR <=2012 and Price >=0 and Price <=100 Limit 0 , 5

This is my query after I change some filters and click submit.

SELECT * FROM Inventory where YEAR >=1970 and YEAR <=2012 and Price >=60 and Price <=80 Limit 0 , 10

This is my query after I click on the 'Next Page' link (back to default).

SELECT * FROM Inventory where YEAR >=1970 and YEAR <=2012 and Price >=0 and Price <=100 Limit 10 , 5

Open in new window

0
 
leakim971PluritechnicianCommented:
not this but the part building your query in your php script
0
 
JohnnyBCJAuthor Commented:
Bingo! You helped me find my problem.

When I was passing the variables to the 'New Page' link, I had a question mark in front of all the variables. I removed the question marks (all except the 1st one) and fixed a couple smaller problems and the way we go!

Thanks again!
0
 
JohnnyBCJAuthor Commented:
My comment is exactly what solved my problem. I was making a typo when creating the 'Next Page' link. This reset the variables to their default values which in return changed the query. The solution to this problem is pretty obvious when looking back at it but hindsight is always 20/20.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 7
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now