If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

How to let user re-sort POST search results passed from previous page

I'm creating a search engine that allows users to search various variables in a mysql database and sort them. I would like the user to be able to RE-SORT the search results they see by any one of the variables, but I'm having trouble getting their search terms passed onto the "next", re-sorted page.

PAGE 2: Displays search results, sorted by "sortfield" in "asc" or "desc". Page 2 contains links with href="?field=differentsortfield" but upon clicking the link, I receive a mysql error that there are no valid variables passed.

Here is my sql query.....

PHP Code:

<? $sql = "SELECT * FROM rentlist WHERE active = 1 AND type LIKE '%". $searchtype ."%' AND location LIKE '%". $searchlocation ."%' AND bed LIKE '%". $searchbedrooms ."%' AND pets LIKE '%". $searchpetok ."%' AND price BETWEEN $searchpricelow AND $searchpricehigh ORDER BY $field$order"; $result = mysql_query($sql); ?>

Is the script you're showing us "index.php", or is it "RentalSearch.php"?

I see you have a script called "RentalSearch.php" ... that processes your form,
yet the links at the bottom for sorting seem to be referencing "index.php"?

The solution to your issue is to use PHP SESSION array to store all of the form
values AND the sort preferences. Once they fill-out the form, those values will
be retained no matter where they go. If they want to change one of them, or
change the sorting, you change the PHP SESSION variable(s).

I'm just not sure if currently you have more than one script?
Because I'm confused about how you are handling the URL variables.

Do all the SESSION (setting variables) in a different script.
Perhaps you might call it "setting.php". You process your form there,
set the SESSION variables and redirect back. That whole transaction
is invisible to the user, but it handles the header stuff you need for
setting SESSION (or cookies).

Your RentalSearch.php script only reads SESSION variables and displays either
the form or the results, or both for the query (which uses those SESSION variables).

When I put this into the RentalSearch.php and then echo $_SESSION['searchtype'] etc, it displays the proper search terms and I get search results. But, upon clicking the sort links it resets the variables and I don't get any results. This makes sense to me - I just put it in RentalSearch.php to echo and test that it worked, which it did.

I moved the script to SearchProcess.php and added a redirect at the bottom

PHP Code:

header( 'Location: RentalSearch.php?field=price&order=desc' ) ;

Now when I search, it runs the script and redirects well, but I get no search results, and when I echo the session variables, they come up blank.