Popular Searches Block

This php code can be used in a drupal block to display the most popular searches on your site.

$query = "SELECT message from {watchdog} WHERE type = 'search'";
$result = db_query($query);
$search_strings = array();
while ($row = db_fetch_object($result)) {
  preg_match('/<em>(.*)<\/em>/', $row->message, $match);
  $string = strtolower($match[1]);
  $search_strings[$string] += 1;
}
if(count($search_strings)) {
  arsort($search_strings);
  $i = 0;
  foreach ($search_strings as $key => $val) {
    if ($i < 10) {
      $i++;
      $query = preg_replace('/\s+/', '+', $key);
      $search_link = l($key,'search/node/'.$query);
      $search_link .= " ($val)";
      $items[] = $search_link;
    }
  }
  return theme('item_list',$items,'','ol');
}

Reply

  • Allowed HTML tags: <b> <br> <p> <a> <strong> <cite> <em> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You may use [img:xx] tags to display uploaded files or images inline.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <css>, <diff>, <drupal5>, <html>, <javascript>, <php>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options