newbtophp Posted August 24, 2009 Share Posted August 24, 2009 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 More sharing options...
AngelicS Posted August 24, 2009 Share Posted August 24, 2009 How to read and write to a file - Trap 17 Tutorial Check it out =) Best wishes //AngelicS Link to comment https://forums.phpfreaks.com/topic/171580-solved-log-latest-searches/#findComment-904865 Share on other sites More sharing options...
newbtophp Posted August 24, 2009 Author Share Posted August 24, 2009 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. Link to comment https://forums.phpfreaks.com/topic/171580-solved-log-latest-searches/#findComment-905093 Share on other sites More sharing options...
AngelicS Posted August 24, 2009 Share Posted August 24, 2009 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 =) Link to comment https://forums.phpfreaks.com/topic/171580-solved-log-latest-searches/#findComment-905143 Share on other sites More sharing options...
newbtophp Posted August 24, 2009 Author Share Posted August 24, 2009 great you solved it! Link to comment https://forums.phpfreaks.com/topic/171580-solved-log-latest-searches/#findComment-905192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.