Forum Moderators: coopster

Message Too Old, No Replies

End pagination session

Got the session sorted now cant get rid of it !

         

wheelie34

6:34 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For a while I struggled with "why" my variable wasnt being carried through the multiple refreshes of result.php so used session_start() and added the variable to the session it works perfect except if the user goes back to search.php and selects a different search criteria, they still view the first choice they made?

I have tried putting session_unset on its own and with session_destroy but while typing them into results.php I realised "this aint gonna work" but tried it to make sure and it doesnt, how do I start a new session as the session is vital to the page refresh type navigation. Thanks

coopster

7:06 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



When they land on the 'search.php' page, start your session and then clear out the $_SESSION index for that variable or better yet, unset [php.net] it after you have finished using it.

wheelie34

7:21 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry if this sounds dumb, but could you tell what I need where, are you saying session_start() at the top of search.php and unset_session() at the bottom?

The user has 3 boxes they "could" choose from 1 or 2 or all 3 how do I use the $_SESSION index you mentioned?

Do I need to add anything to results.php to do with the session?

Thanks again

coopster

7:31 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



No problem. And I just read your message again -- it seems you added $_SESSION management as a last ditch effort to maintain the search throughout your "results.php" processing. I'm guessing you are using pagination or something and want to keep the "search keywords" in your query as you page through the result set, right? There are a few ways to do this, sessions being one of them. But you probably don't need a session to do this. You can carry the search through the GET query string (the part that shows up in the browser address bar, like at google.com) or you can use a hidden form field too. Just keep grabbing the value and pushing the name/value pair back into your query string or hidden form field on each invocation of your "results.php" script.

If you want to continue using the $_SESSION option that you have implemented then you are correct. You need to start your session at the beginning of the "search.php" script and unset your $_SESSION['mySearch'] variable. That way it won't be carried into your "results.php" script (or whatever script is named in the action attribute of the "search.php" form).

wheelie34

7:50 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Coopster, I think the form hidden field will be the best as you say, I didnt think of it, only worked with the session, you are correct in pagination, the first db call sets the value and amount of links/pages then the page html start and a second call to the db for the same details is made for the content, that call has LIMIT within it for th pagination.

Where do I put the form hidden field and how do I keep it there during the refreshes, sorry for the need of spoon feeding, do you mean set the form method on search.php to "get" rather than "post" or the hidden field for method, and, is that on the results.php page.

How would I push/pull the value from field to field, the links I am using are text rather than buttons.

coopster

8:00 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



All you should have to do is a little bit of work in your "results" page. When you first land there you must be grabbing the search criteria, correct? Coming from the "search" page you are using $_GET or $_POST? Let's say you are using $_POST. Now, when you get to the "results" page you must be pulling that data in order to form your query. Something along the lines of ...

$search = $_POST['search']; 
$sql = "SELECT * FROM table WHERE searchField LIKE '%" . mysql_real_escape_string($search) . "%'";

Well, now all you have to do is make sure you also put that into your "results" form page too.

<form method="post" action="results.php"> 
<input type="hidden" value="<?php print htmlentities($search); ?>" />
</form>

Or something along those lines. Make sense?

wheelie34

8:08 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Will give it a try tomorrow morning first thing and post a reply either way, I understand the logic, just not 100% on the action to take, will let you know.

Thanks agains

wheelie34

8:20 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I ahve added this to the results.php page within the html and just above the opening php tag where the call is made from, when I check the source of the first result page it has the variable within the hidden field, when I click next its gone, I cant understand how the hidden field gets passed when a hyperlink is clicked is there a way of making it save its state, have I missed something?

<form method="post" action="date_search_results.php">
<input type="hidden" name="start" value="week_1" />

search.php sends the result of a select box, so theres no string checking, I know whats coming in, I use this pagination script on many pages throughout manysites and thisis the first time it has caused a problem, this could be the reason, the db call isnt the usual

select what from table where column = $string
the column name is actually whats passed from the search page, all otherpages where the script works without loosing the data have the usual where column = $string, is that the cause of this issue?

coopster

8:36 pm on Oct 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




I cant understand how the hidden field gets passed when a hyperlink is clicked is there a way of making it save its state, have I missed something?

There it is. You aren't using a form with a post method, you are using a hyperlink, just like at google. Have a closer look at how google carries the search term from page to page. As you hover over the "next" link, look at the status bar in your browser. You see the "search" keyword along with it's value in the link? That is how the state is being maintained. Scrap the hidden form field and add the search query to your hyperlink and you will maintain the state.

wheelie34

7:46 am on Oct 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Coopster the navigation was the problem, I had reversed it so the $start was hard coded in the first set of links generated and lost on the next page.

Thanks very much

coopster

5:35 pm on Oct 24, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hey, glad you got it sorted. I try to avoid hard-coding as much as possible. Sure can make things difficult sometimes as you now understand ;-)