php mysql search query
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开发者_JAVA百科 do you recommend?
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.
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!
use 2 search queries, 1st as select count(1) from _table_ WHERE _where_
and second one as select _fields_ from table where _where_ LIMIT _limit_
where _limit_
is $pagenum*$itemsPerPage, ($pagenum+1)*$itemsPerPage
精彩评论