tips and tricks for movable type
recent searches using PHP and MySQL
May 02, 2003
I implemented some code from Brenna to display recent searches in my sidebar.
One thing which kept me from using this code on my site before is that the Activity Log (where MT records the search requests, and thus, where this script pulls info from) is not blog-specific; results are from all blogs where the search feature is offered.
My solution to this (though not a perfect one) was to modify the resulting search links so that they use my alternate template for the Tips blog, as well as specifying this blog's ID. The imperfect part is that the list will still include searches that were on my journal (for example), but clicking on the link will most likely give "no results found". I figure most people visiting here aren't going to be interested in those other searches anyway. Heh.
So, here's the code if you're curious.
// connect info
include ("/path/to/your/connection/file/connect.php");
// config
$cgi_path = "<MTCGIPath>mt-search.cgi";
$show = 10;
$getlogs = "SELECT log_message, log_created_on FROM mt_log ORDER BY log_created_on DESC";
$result = mysql_query($getlogs);
$i = 0;
while ($row = mysql_fetch_array($result)) {
$pos = strpos(($row['log_message']), "Search: ");
if ($pos !== false) {
$logs[$i] = str_replace("Search: query for ", "", ($row['log_message']));
$logs[$i] = substr($logs[$i], 1, -1);
$i++; if ($i == $show) {break;}
}
}
echo "<div class=\"sidetitle\">Recent Searches</div>";
echo "<div class=\"side\">";
for ($i=0; $i<$show; $i++) {
echo "• <a href=\"$cgi_path?search=$logs[$i]&Template=tips&IncludeBlogs=8\">$logs[$i]</a> <br />";
}
echo "</div>\n";
?>
You must change /path/to/your/connection/file/connect.php to point to your connect file; and modify &Template=tips&IncludeBlogs=8 or remove it if it doesn't fit your needs.
Related Links:
• top searches using PHP and MySQL
• MT Searches plugin
TrackBack: 8
(URL: http://www.thegirliematters.com/sf/mt-track.cgi/147)
» Fancy Web Stuff
Excerpt: So, I have been suffering from Plugin Mania(TM) and have installed and run tons of them in the past month. Thing is, they didn't always work. All of them do now, though. :smile: For a quick re-cap: First I installed...
Weblog: Dark Star
Tracked: 07.23.03 09:38 PM
» Bloggie Scripty Reverb
Excerpt: I like to keep an up-to-date listing of all the...
Weblog: Bloggie Broad
Tracked: 12.16.03 06:07 PM
» MT Searches Plugin
Excerpt: As posted at The Girlie Matters: Why don't you use the MT Searches plugin for this? I recently implemented this (in a multi-blog environment) and it works great. See it in action at http://www.stanthecaddy.com/search.html. (Note that in this implementa...
Weblog: Mark Carey
Tracked: 01.05.04 04:22 PM
» Faithjean dot com's photolog is up!
Excerpt: Thanks to Gabriel for helping me:-) 2 do list: *The comment on this page doesn't work right now! Hopefully I'll figure it out soon...:-) Scripts: *Add: Most commented - entries & Most commented - visitors script *Last 50 referrers(pop up)...
Weblog: .::faithjean.com::.
Tracked: 01.06.04 05:30 PM
» Bloggie Scripty Reverb
Excerpt: BB's Script, Plugin and Hack Archive as of 3/1/04: Collapse...
Weblog: Bloggie Broad
Tracked: 03.01.04 05:41 PM
» Recent / Top Search Script Modification
Excerpt: 1/13/04 REVISION: I caught an error in the following code...
Weblog: Bloggie Broad
Tracked: 04.02.04 06:55 PM
» Recent / Top Search Modifications
Excerpt: I made an easy modification to Girlie's top and recent...
Weblog: Code Novice
Tracked: 04.14.04 11:35 PM
» Tweaking Searches
Excerpt: Updated 09/05 - Search Throttle The search form that comes with default MT templates is very limited in what it lets you do - basically it only lets you search the current blog and allows you to not specify any...
Weblog: Movalog
Tracked: 09.05.04 05:25 AM
Comments
hmm..
Why don't you use the MT Searches plugin for this?
I recently implemented this (in a multi-blog environment) and it works great. See it in action at http://www.stanthecaddy.com/search.html. (Note that in this implementation I also limit the results to a single category).
The reason I don't use the plugin is very simple: it's not dynamic. You have to rebuild your site to get the most recent searches. ;-)
I guess that's a good reason - depending on the intent and frequency of rebuilds. It works well for me on the Seinfeld Blog because rebuilds are very frequent, due to the MT-powered forum on the site. :-)
the line below
($cgi_path = "mt-search.cgi";)
does that mean i have to create a new file called mt-search.cgi so that it could put all the search entries into that file. or am i just talking rubbish.