Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search within a search - session variables?

Status
Not open for further replies.

peasepud

Programmer
Jul 20, 2007
35
Hi,

I've been a member here for years but have lost both my password and original email address lol so dbaseboy is now peasepud.

Anyway, Im trying to allow users to search a database get a set of results and then search again to narrow down the results (basically Googles Search within results facility) and on and on etc.

Ive tried a few things to no avail and I really dont want to pass a huge query within the URL (but will if you say its best) so Ive been searching for a better way and came across sessions. Would it be possible to store the query at each stage in a session variable then just add to it for the new search? I've searched google a bit and played around but keep getting header errors. Any help to lead me down the right path would be gratefully appreciated.

Cheers

Peter
 
the answer is not really.

the results of a query is just a resource handle. you cannot serialize a resource handle so cannot put it in a session variable.

what you can do, is to store the query itself in a session variable.

you can also store the entire resultset in a session variable if you first read the resultset into an array.
 
I think we're saying the same thing, maybe I didnt make it very clear though.

What I was thinking was starting with a $querystring of

"SELECT * from table"

then if you research on say age then the $querystring would become

"SELECT * from table WHERE age > 30"

then maybe go for height < 70 inches to make it

"SELECT * from table WHERE age > 30 AND height < 70"

each stage saving the querystring as it stands in the session. So when the results page is viewed it uses the querystring from the session.

ie $result=mysql_query($querystring)

Is that what you meant as well?
 
Thats easy enough.

You'll need to just add the current query to the Session, so its maintain, then just pull it out, and attach whatever else you want to it.

Code:
session_start(); [green]\\Required on each page you wish to use sessions[/green]

[green]\\check if you have a query stored:[/green]

if(isset($_SESSION['myquery'])){
$querytouse=$_SESSION['myquery'];
}



Than wherever you are parsing th new values for the query, you can do something like:

$new_query=$querytouse . " AND age > 30 ";

[green]Then just run the query.[/green]

$res=mysql_query();
...


[green]\\When you are done with all the processing, you can store the new query into the session variable[/green]

$_SESSION['myquery']=$new_query;


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
that was what I meant, yes.

in terms of practice I would store each part of the WHERE clause in a separate array element and distinct from the main body of the query. you then join the array elements at the end as so

Code:
foreach ($_SESSION['where'] as &$where){
 $where = "'".mysql_escape_string($where)."'";
}

$where = "WHERE " . implode(' AND ', $_SESSION['where']);

$query = "Select * from table $where";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top