Ajax Search

Frågor kring MODdar (modifikationer) för phpBB 3 (3.0.0 eller senare).

Moderatorer: Moderatorgrupp, Supportgrupp

Skriv svar
Användarens profilbild
Austin
Medlem
Medlem
Inlägg: 23
Blev medlem: 2006-07-21 14:28
Svar: 0
Ort: Falköping

Ajax Search

Inlägg av Austin »

Hej
Har hittat en kul pryl, som skulle vara trevligt att ha på vårat forum.

http://www.mattking.org/2007-08/ajax-se ... lugin.html
Skriv Test i sökrutan och se... :)

Se även: http://www.mattking.org/ajax.search/


Koden för ajax.search.php är:

Kod: Markera allt

<?php
/*
Plugin Name: AJAX.Search
Plugin URI: http://www.mattking.org/ajax.search
Description: Creates an AJAXy search box for your blog
Version: 0.1
Author: Matt King
Author URI: http://www.mattking.org
*/
/*  Copyright 2007  Matt King  (email : matt@mattking.org)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

include_once(dirname(__FILE__) . '/../../../wp-config.php');
include_once(dirname(__FILE__) . '/../../../wp-includes/wp-db.php');

if ($_POST['search']) {
  echo search_posts($_POST['search']);
  exit;
}

function search_posts($search) {
  global $wpdb;
  $search = $wpdb->escape($search);
  $res = $wpdb->get_results("select ID, post_title from {$wpdb->posts} where post_status = 'publish' and (post_title like '%$search%' or post_content like '%$search%') limit 0,30");
  $output .= "<div id=\"search_results_top\"></div>\n";
  $output .= "<dl>\n";
  if (count($res)) {
    $total_count = 0;
    foreach($res as $r) { // gather results into arrays grouped by category
      $post_categories = get_the_category($r->ID);
      foreach($post_categories as $cat) {
        $posts[$cat->cat_name][] = $r;
        $total_count += 1;
      }
    }
    ksort($posts);
    $output .= "<dt class=\"search_results_header\">Category</dt>\n";
    $output .= "<dd class=\"search_results_header\">$total_count Post" . ($total_count != 1 ? 's' : '') . " Found</dd>\n";
    foreach($posts as $cat_name => $posts) {
      $output .= "<dt>$cat_name (" . count($posts) . ")</dt>\n";
      foreach($posts as $post) {
        $output .= "<dd><a href=\"" . get_permalink($post->ID) . "\">{$post->post_title}</a></dd>\n";
      }
    }
  } else {
    $output .= "<dt>No Results</dt>\n";
    $output .= "<dd>Nothing found for search \"" . stripslashes($search) . "\"</dd>\n";
  }
  $output .= "</dl>\n";
  $output .= "<div id=\"search_results_bottom\"></div>\n";
  return $output;
}

function load_ajax_search() {
  $siteurl = get_option('siteurl');
  echo "<script type=\"text/javascript\" src=\"$siteurl/wp-content/plugins/ajax.search/js/prototype.js\"></script>\n";
  echo "<script type=\"text/javascript\" src=\"$siteurl/wp-content/plugins/ajax.search/js/ajax.search.js\"></script>\n";
  echo "<link rel=\"stylesheet\" href=\"$siteurl/wp-content/plugins/ajax.search/css/default.css\" media=\"all\" />\n";
  echo "<script type=\"text/javascript\">\n";
  echo "Event.observe(window,'load',function() { new Ajax.Search({\n";
  echo "    url: '$siteurl/wp-content/plugins/ajax.search/ajax.search.php',\n";
  echo "    input: 's'\n";
  echo "  });\n";
  echo "});\n";
  echo "</script>\n";
}

add_action('wp_head', 'load_ajax_search');

?>
Hurpass svårt skulle det vara att göra om det till en phpbb3-plugin?

Resten är en tillhörande js-fil (i princp)

Tänkte det kunde vara bra o ha den som sökruta - o sen en länk till "sök" som ger den vanliga "avancerade sökningen"
Skriv svar

Vilka är online

Användare som besöker denna kategori: 1 och 0 gäst