Jump to content

[SOLVED] Log latest searches


newbtophp

Recommended Posts

Im trying to log all searches to a txt, and then echo 10 random searches by fetching the words from the .txt

 

The issue is i find it harder, since my search engine is not using a database its just using the yahoo api.

 

Heres the search form:

 

<form method="get" action="search.php">
<input name="search" type="text" class="input-box" />//Whatever is searched here is logged
<input name="submit" type="submit" value="Search" />

</form>

 

Link to comment
https://forums.phpfreaks.com/topic/171580-solved-log-latest-searches/
Share on other sites

This is what I've come up with:

 

<form method="get" action="search.php">
<input name="search" type="text" class="input-box" />//Whatever is searched here is logged
<input name="submit" type="submit" value="Search" />

</form>

<?php
$thefile = "name.txt"; 
$towrite = $_POST["search"]; 

$openedfile = fopen($thefile, "w");
fwrite($openedfile, $towrite);
fclose($openedfile);
?>

 

I still dont know how to echo the contents of name.txt as a tag cloud.

I'll show you how to make it with a MySQL database =)

 

Create a new table, named searches.

 

Inside this table, add 2 fields.

 

id | int | 30 | primary key | auto increacement

name | varchar | 65

You should make sure that you make a variable for the thing they search for. In this case, I'll name the variable $searched_for

Now, when a user presses the search button, run the following query.

 

mysql_query("INSERT INTO `searches`(`name`) VALUES('{$searched_for}')") or die(mysql_error());

 

Now when you want to show the 10 latest searches, you can do a for loop, like this:

 

$select=mysql_query("SELECT * FROM `searches` ORDER BY `id` DESC") or die(mysql_error());
for($i=1;$i <= 10;$i++)
{
$row=mysql_fetch_array($select);
echo $row['name'];
}

 

I think this should work. Not tested, but try it, and tell me if any errors occurs =)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.