Question

I'm new to post/redirect/get. I'm coding up the first real site that led me to discover the need for PRG. So I had written and gotten working code that did the following:

1) user enters a search string

2) we search the database and find their desired search results

3) if we found their search results successfully, we alter the database -- 
   a 'frequency of lookups' -- to indicate the user searched and found what 
   he was looking for

4) then display the results he searched on

What I found was refreshing the page in the browser resulted in the user seeing his same search results again but we incremented the 'frequency of lookup' -- the user is limited in the frequency of lookup in the database -- so if the user refreshed the page too many times, they ran through their hourly ration of database lookups. That's when I discovered Post/Redirect/Get a few days ago.

I am now going through the site and altering all pages that alter the database and display results and switching them over to PRG. Here is the new process:

1) user enters a search string

2) we search the database and find their desired search results

3) if we found their search results successfully, we alter the database -- 
   a 'frequency of lookups' -- to indicate the user searched and found 
   what he was looking for

4) PRG to a 'results' page

5) then display the results he searched on

And I ran into a problem. I need to somehow 'pass' their search results from step (2) above to the new 'results' page in step (5) that I created to implement PRG.

So my question is -- is there a 'most common' way to do this? In reading around I've seen 'save the results in a .CSV file' and also 'save the database search results in the SESSION.'

I want to save the search results in the SESSION then on my 'GET' page I added for PRG to display the result, I'll read the search results from the session variable and display them.

By that I mean I want to do this:

$result = mysql_query($query, $theDbServer);
$_SESSION['theSearchResults'] = $result.

Then on the 'display' page, read back the search results from $_SESSION['theSearchResults'] and display them using:

 $result = $_SESSION['theSearchResults'];
 $row = mysql_fetch_row($result);

Is this a normal approach? And I suspect I cannot save the raw $result in a session variable like the above -- but I'm also not sure how to put the $result above into the $_SESSION -- how is that normally done?

Was it helpful?

Solution

Assuming you don't want to have the final landing page do the querying (without decrementing their quota of course), then you're going to need to use session.

Saving to a CSV is not standard and wouldn't scale very well. I would parse the results of the query into a more user-friendly form (a simple class or list or whatever you need). I'd then store the class and not the reader into the session. You'll probably want to clear out that portion of the session when they leave the results page (especially if it's a huge amount of data).

This is assuming you're using raw php. There are many frameworks with features for this exact case (you want to shuttle a piece of data from one page to the next).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top