Php show recent searches

I can create the cookie no problem, but I can not update it and store more than 1 search. Indeed.co.uk they store the searches in the Cookie in the below format:

"q=carpenter&l=london&ts=1506177856392:q=bricklayer&l=Southwark&ts=1506008036065:q=labourer&l=Southwark&ts=1506007988029" 

As you can see Indeed store multiple UNIQUE searches.

I am using PHP does anyone know how to replicte how Indeed store the user form searches? I need to bring the queries as an Array.

Yes spot on, I want to save all unique form searches for job site, so I can display «My Recent Searchs»

Zaaayymn bro! I was writing the code but another answer was first. Anyway, indeed separates them with a colon. Plus what Jimmmy said.

1 Answer 1

To store more searches into one cookie, first you have to read actual value from the cookie, then add new search.

 else < $searches_to_store = $new_search_value; >setcookie($cookie_name, $searches_to_store, time() + (86400 * 30), "/"); 

this code checks whether there exists your cookie and if so, parses all searches into array. Then adds new search and implode into cookie string. Searches are delimited by | (pipe) character Then saves it..

I would however suggest to save those data about searches into session values, its stored on server and can’t be manipulated by user

$_SESSION['searches'] = $searches_to_store; 

EDIT: to store only unique searches, can assign stored searches into array by some key.

//after explode $searches_by_key = array(); foreach($searches as $search)

by this you will have unique keys in $searches_by_key, that you can store into cookie

Источник

How to display recent queries in get method

If entered one name «google» after that again I entered «example» is there any way by which I can display recent entered names when I type another?

Save them in session or in database. Also, won’t send the form. You need to add the type=»submit» attribute.

1 Answer 1

the code that i think could be enough to handle the issue would be

 session_start(); $name = $_GET["name"]; $_SESSION["list"][sizeof($_SESSION["list"])] = $name; var_dump($_SESSION["list"]); //I used var_dump here only to show the output you are free to use any other //way to display the data as you like 

that really doesnt matter much just change the POST with the GET and it will get the work done. Wait i will edit the codes up for you

Читайте также:  Vodo kanal ru pok action php

also here I am getting url at top is example.com/?name=google and i want custom url as example.com/name/google

@AishwaryaTaneja this should take care of the issue but i must warn you that these changes are not persistant and will be erased once the user closes his browser. If you want the changes to be persistasnt you should consider using a database to store all the entries

@AishwaryaTaneja for custom routes you should have a look at URL routing for that you will need to configure your htaccess file in apache and rather than getting from the GET request in PHP get the complete requested URL and parse that to get the request parameter. Here is a script i wrote a little while back that implements url routing also github.com/georoot/finna-ninja/blob/master/index.php

Источник

How would I log searches on my PHP/MySQL website?

I have a table searchterms at the moment with two fields: id and terms , I was wondering if this was the best method of logging this info. Terms is becoming a bit redundant however.

any clues or links to relevant scripts?

both useful answers but im going to look into the Google analytics first. Ill do anything to minimize server load!

2 Answers 2

I haven’t tried but google analytics provides the option to track what users enter in your search box. For displaying there’s the developer API.

The caveat is this would all be javascript. The advantage is that it would lessen the load on the server.

Maybe not what you were looking for but worth mentioning.

what about a table to store the terms (maybe in some «normalized» way, like all lowercase, no accents, . ), and another table to store when those terms have been searched for ?

Normalization thing is to avoid «Word» and «word» being seen as two different terms in your could ; same for «éléphant» and «elephant», btw.

terms - id - word terms_searched #id _term - time_search 

Then, knowing the last searched terms, or the most searched terms, is just a matter of a simple SQL query, like

select id, term, count(*) from terms inner join terms_searched on terms_searched.id_term = terms.id 

Maybe with a where clause to specify you only want recent searches to be taken into account ?

Читайте также:  Java downloads for mac

And if your tables become really big and the join in the query starts hurting performances, you can envisage denormalization, putting a «num_times_searched» in terms table, that sums up how many times a term has been searched for

Источник

PHP remember last search phrases

I have seen many sites doing that. You can see search phrases under the search field. Which is the best way to implement that?

What exactly do you want? Do you want to show recent searches of the current user or show the most popular searches?

Do you mean recent search phrases per user or something like autocomplete with recommended/often searched phrases from all users together?

3 Answers 3

If you want to display the most recent searches, just save those searches anywhere. In your database for example (or even a plain text file would work). Create a table recent_searches and save the latest queries in there. Use a cronjob to clear the table once in a while (if you want). When performing a search, you can insert it into your recent_searches table, for example:

$searchterm = 'Apple Pie Recipe'; // Insert the search into your recent search table mysql_query("INSERT INTO `recent_searches` (`term`) VALUES ('" . mysql_real_escape_string($searchterm) . "'"); // Then search. mysql_query("SELECT * FROM `searchtable` WHERE `title` LIKE '%" . mysql_real_escape_string($searchterm) . "%'"); 

To retain the search fields after page gets reloaded, You are querying the database based on search fields

$mysearchStr =$_POST['searchPattern']; if(empty($mysearchStr) < echo''; > else < echo''; > 

I guess the solution will vary a bit depending on how advanced you want to do this. If you only want to show the most popular searches, then I guess you can just have on table in your db and save the complete search term in one column, and have a second column where you do +1 every time someone search for that exact term. If the search term isn’t already in your table, you add it.

Читайте также:  Php функция принимает массив

That is a bit limited however. If you want to to be able to show trends for instance, or if you want to show most popular searches today, I guess a better way would be to use two db-tables. Your first table stores an id and the complete search term. Your second table store one row for every search, and contain an ID of the search term used, and a time-stamp for when the search was done.

Using the second alternative would give you a whole lot of more possibilities, as you know the date and time of every search done one your site.

Источник

Get most recent files recursively with PHP

I am looking for code which lists the five most recent files in a directory recursively. This is non-recursive code, and would be perfect for me if it was recursive:

', GLOB_BRACE ); usort( $files, 'filemtime_compare' ); function filemtime_compare( $a, $b ) < return filemtime( $b ) - filemtime( $a ); >$i = 0; foreach ( $files as $file ) < ++$i; if ( $i == $show ) break; echo $file . ' - ' . date( 'D, d M y H:i:s', filemtime($file) ) . '
' . "\n"; /* This is the output line */ > ?>

What do you mean recursively? You want to list the five most recently touched files in a given directory and all of its subdirectories?

Yes, yes! «list the five most recently touched files in a given directory and all of its subdirectories»

4 Answers 4

This was my first version (tested, working):

function latest($searchDir, array $files = array()) < $search = opendir($searchDir); $dirs = array(); while($item = readdir($search)) < if ($item == '.' || $item == '..') < continue; >if (is_dir($searchDir.'/'.$item)) < $dirs[] = $searchDir.'/'.$item; >if (is_file($searchDir.'/'.$item)) < $ftime = filemtime($searchDir.'/'.$item); $files[$ftime] = $searchDir.'/'.$item; >> closedir($search); if (count($dirs) > 0) < foreach ($dirs as $dir) < $files += latest($dir,$files); >> krsort($files); $files = array_slice($files, 0, 5, true); return $files; > 

But I like byte’s usage of glob() , so here is a slightly modified version of his to return the same format:

function top5modsEx($dir) < $mods = array(); foreach (glob($dir . '/*') as $f) < $mods[filemtime($f)] = $f; >krsort($mods); return array_slice($mods, 0, 5, true); > 

This returns the time (UNIX Timestamp format) that the file was modified as the key of the element in the array.

Источник

Оцените статью