Recent Searches - Help please. (my first post!)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHPAddiction
Forum Newbie
Posts: 1
Joined: Thu Nov 18, 2010 11:12 pm

Recent Searches - Help please. (my first post!)

Post by PHPAddiction »

Hello All. I hope I am doing this right. This is my very first post here and I am pleased to join the PHP DN forum. I am a web designer who has recently learned PHP (my first language other than css/html of course), and I feel absolutely addicted to learning. I've actually done alright for myself since I learned how to create PHP Pages on the fly already and I just started learning 3 days ago :) But I'm a little stuck and need a little help. I promise 100% that I have done TONS of research about this, just can't find my answer. Anything is appreciated, and thank you in advance!

I am trying to create a function that will echo the most recent searches on my search engine. However here is what I need to achieve:

1. Don't echo duplicate recent searches. Every search made is logged to my database, including duplicates.
2. They should not exceed a certain amount of characters as I have these recent searches displayed on a small horizontal bar that goes across at the absolute top of my search engine. Too many characters might make it look bad (small monitors, small resolutions), especially because I have additional links on the far right of the horizontal bar. So in case somebody makes a query for a very long phrase, don't echo that phrase.
3. However, there should always be enough recent searches so that it doesn't look too empty! So if there's 3 searches that are all 1 letter long (hypothetically) make sure to echo additional recent searches!
4. Only repopulate (is that the right word?) the recent searches every minute or so. I will be getting very heavy traffic and don't want to put unnecessary stress on the server.
5. Finally, don't echo recent search queries that would not return a result on the search engine. (My search log has a field titled "results" which lists how many results the search query returned)


Here's what I've started with (apparently I did the implode incorrectly as it does echo the recent searches but without the glue).

function recent_searches(){
$query = mysql_query("SELECT search_string FROM search_log ORDER BY datetime DESC LIMIT 3");
while($row = mysql_fetch_row($query)){
echo implode(", ",$row);
}

}

recent_searches();



I know this is not complete. But I don't know how close I am either. I'm not even sure how difficult this is. But once again, ANY help in the right direction is a major assistance to me while I continue to explore the depths of PHP! ^_^ Thank you all for reading.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Recent Searches - Help please. (my first post!)

Post by mecha_godzilla »

I'm not sure whether this will help or not, but when I parse MySQL queries I usually do it like this:

Code: Select all

$query = "SELECT * FROM some_table";
$result = mysql_query($query, $conn) or die(mysql_error());
$number_of_results = mysql_num_rows($result);

if ($number_of_results != 0) {

while ($result_array = mysql_fetch_array($result, MYSQL_ASSOC)) {
				
    $some_value = $result_array['some_value'];

    echo $some_value;

}

}
You might want to consider whether you want to record duplicate (identical) searches in the first instance - identifying them at this stage might be quicker than getting MySQL to ignore them when you want to generate your "recent searches" list; however you can use SELECT DISTINCT as an alternative to this. To avoid returning very long search terms, you can either put a character length limit in your query so MySQL doesn't select them or you can truncate the search terms afterwards (it would be better to truncate the search terms to identify the spaces between words rather than just using an arbitrary length of, say, 40 characters).

To only repopulate the "recent searches" list every so often, you could either save the results into a text file or in the database itself. If you add a timestamp to this, then whenever someone visits your site the function can check to see what the timestamp is and retrieve new results if they are older than 1 minute ago.

HTH,

Mecha Godzilla
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Recent Searches - Help please. (my first post!)

Post by Celauran »

Maybe something like this?

Code: Select all

SELECT DISTINCT(LEFT(search_string, 30))
FROM search_log
WHERE results > 0
ORDER BY datetime DESC
LIMIT 3
Post Reply