Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store search result?

I am working on my personal site, where I want to store my customers recent search result limited to that particular session.

I am using PHP platform and Javascripts.

Here is an example of what I am exactly looking at :

It stores your previously searched domain name for that particular session so that user can make decision by comparing those results.

Thanks.

EDIT- Well Thanks for all of your answers and suggestions.

But If you have noticed above example

It looks like some kind of script loading a new content on the same page without refreshing it and keeping previous search content <div> as it is.

How to achieve this using javascripts or some sort of div layer ????

like image 692
MANnDAaR Avatar asked Dec 04 '25 01:12

MANnDAaR


1 Answers

UPDATE START

This example uses page reload. If you want to do it without page reload, you can but you'll have to use AJAX to load new search results. But then, it's not a PHP question. I suggest looking at jquery library, as it makes it easy. Tutorials: http://docs.jquery.com/Tutorials and e.g. this one ( http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax ).

When loading data via AJAX, the page rendering result (in my example search.php) should return only HTML for results part, not whole HTML page. This is generally a first part of my tutorial (without session).

But I really think that AJAX in here is not really needed. Session is more reliable and allows access to your page from older / mobile browsers where not always JS works correctly.

UPDATE END

Ok then. Let's try the simple tutorial then. Sorry if too simple, but I don't know your exact level.

PHP has mechanism called sessions. In reality they are just bytes stored on server. Server knows which session is for each client by reading session cookie from client browser.

Not every page uses sessions (not every page needs it, and session uses server space, even if only temporarily), session is not enabled by default. To turn on session you use command

<?php session_start(); ?>

In most cases this is either run by PHP framework you use, or put near the top of your site. Session is definitely needed if you want to authenticate user somehow. Or in your case :)

To access session you can use superglobal $_SESSION variable (superglobal means that you can access it anywhere). It's an array, so session element will be e.g. $_SESSION['search'] etc.

As example, let's assume that your page looks like that

<html>
...
<form action="search.php" method="post">
Search: <input type="text" name="searchQuery" />
<input type="submit" value="Search" />
</form>
...
</html>

this very form will send user search to file named search.php. It can be the same file where the form resides - in simplest case when you put both your code and HTML in one file. Beginners often use this schema, although it's not advisable as result is a mess and hard to further change.

In search.php then, you'll use similar code:

<?php
if (!empty($_POST['searchQuery'])) //we have a new search
{
  $result = do_search($_POST['searchQuery']);
}
?>

Then, somewhere below you'll display your search result ($result variable). do_search() function is your search mechanism, I guess you have it somewhere. You may have it not 'wrapped' in a function, then I advise to create it like that, it's much more useful.

function do_search($searchQuery)
{
  ...
  return $result;
}

mind it, the above code doesn't use sessions yet. Let's add saving previous search results in session. The code may then look like that:

<?php
session_start(); //Starting session

//let's create session variable used to store results
if (!isset($_SESSION['searches']))
  $_SESSION['searches'] = array();

if (!empty($_POST['searchQuery'])) //we have a new search
{
  if (isset($_SESSION['searches'][$_POST['searchQuery']]) //User already searched on this value, delete previous result from sesion
  {
    unset($_SESSION['searches'][$_POST['searchQuery']]);
  }
  $result = do_search($_POST['searchQuery']);
  //Let's add new search on the begining of session array to make iterations easier.
  $result = array($_POST['searchQuery'] => $result); //convert result to same format as session table
  $_SESSION['searches'] = array_merge($result, $_SESSION['searches']);
}
?>

In display you'll now not iterate on $result variable as before, but instead you will do something like

foreach ($_SESSION['searches'] as $query => $result)
{
  ...//display of single result
}

I haven't tested following code and it's not a full program. Parts to display result and to do actual search are not described but I guess you have them already prepared. Also, this is only one possible approach of countless possibilities. But I hope this helps :)

Possible modification - now I always perform search, even if user already searched on this term. You may want to receive the result from cache without second search. Then the code will look like

if (isset($_SESSION['searches'][$_POST['searchQuery']]) //User already searched on this value
{
  $result = $_SESSION['searches'][$_POST['searchQuery']];
  unset($_SESSION['searches'][$_POST['searchQuery']]);
}
else
{
  $result = do_search($_POST['searchQuery']);
}

For more in-depth information about sessions and some other constructs used in my example I suggest PHP manual

http://pl.php.net/manual/en/book.session.php

and various tutorials over the network. Or you can add a comment here :)

like image 101
Tomasz Struczyński Avatar answered Dec 05 '25 15:12

Tomasz Struczyński



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!