I have a search form regarding property search having some elements on which search will give results. eg area,location,city,no of bedrooms etc. once the form is submitted i can get the values of all form elements by means of $_post. Problem is that when i will implement paging, then how will i get the values of elements on next pages? should i use some sort of global variables/array or sessions? what do you recommend?
3 Answers
To get the values of $_post on next page you have to pass that $_post values in pagination link.
Say for example you have one input field named as search on which you are performing search. so you have to pass $_POST['search'] on each pagination link.
<a href='search.php?page=2&search="'. $_POST['search'].'"'>Page 2</a>
Now on Page 2, you can get $_GET['search'] and use it in your query
==============================================================
You can use session also
when you get your $_Post values after submit, start session and save it in session variable. In that case you dont have to pass that value in Pagination link
session_start();
$_SESSION['search'] = $_POST['search'];
Now you can directly use $_SESSION['search'] value in your query.
But for every new search make this session variable empty, so if search field do not have any search value then it dont search in database with its older value.
2 Comments
I recommend that you, in your mysql-query use LIMIT. Such as for the first page, if you want 10 results:
SELECT * FROM searchtable LIKE %%WHATEVER%% LIMIT 0,10
This will limit the results to 10. For page 2 you sage:
SELECT * FROM searchtable LIKE %%WHATEVER%% LIMIT 11,20
Of course what you are going to do is set the limit as a variable from PHP. Hope this helps!