Thread: Ajax Search
View Single Post
  #1  
Old 20th November 2010, 21:54
lateam's Avatar
lateam lateam is offline
Senior Member
 
Join Date: Apr 2009
France
Posts: 40
Default Ajax Search
attention

Attention

coding in course






1. create the plugin

Plugin Name:TESTLATEAM.php

Plugin Title:Rechercher un Torrent

Plugin Content:
PHP Code:
<script src="ajaxsearch.js"></script>
<form name="form1">
<input name="keyword" onKeyUp="SendQuery(this.value)" style="WIDTH:100px" autocomplete="off">
<div align="left" class="box" id="autocomplete" style="WIDTH:200px;BACKGROUND-COLOR:#EDF3DE"></div>
</form>
<script language="JavaScript"> HideDiv("autocomplete"); </script> 
2. create root/ajaxsearch.js

PHP Code:
// AjaxSearch 0.1
// Adding Ajax-like search into your ts 5.6
// By lateam

 
var req;
 
function 
Initialize()
{
    try
    {
        
req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(
e)
    {
        try
        {
            
req=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(
oc)
        {
            
req=null;
        }
    }
 
    if(!
req&&typeof XMLHttpRequest!="undefined")
    {
        
req = new XMLHttpRequest();
    }
 
}
function 
SendQuery(key)
{
    
Initialize();
    
    var 
url="ajaxsearch.php?s="+key;
 
    if(
req!=null)
    {
        
req.onreadystatechange Process;
        
req.open("GET"urltrue);
        
req.send(null);
 
    }
}
 
function 
Process()
{
    if (
req.readyState == 4)
    {
    
// only if "OK"
        
if (req.status == 200)
        {
            if(
req.responseText=="")
                
HideDiv("autocomplete");
            else
            {
                
ShowDiv("autocomplete");
                
document.getElementById("autocomplete").innerHTML =req.responseText;
            }
        }
        else
        {
            
document.getElementById("autocomplete").innerHTML=
                
"There was a problem retrieving data:<br>"+req.statusText;
        }
    }
}
 
function 
ShowDiv(divid)
{
   if (
document.layersdocument.layers[divid].visibility="show";
   else 
document.getElementById(divid).style.display="inline";
}
 
function 
HideDiv(divid)
{
   if (
document.layersdocument.layers[divid].visibility="hide";
   else 
document.getElementById(divid).style.display="block";
}
 
function 
BodyLoad()
{
    
HideDiv("autocomplete");
    
document.form1.keyword.focus();

2. create root/ajaxsearch.php

PHP Code:
<?
    
// AjaxSearch 0.1
    // by lateam
  
    
    
    
$config['dbhost'] = "localhost";        // Database host
    
$config['dbname'] = "xxxxxxxxx";        // Database name
    
$config['dbusername'] = "xxxxxxxx";            // Database username
    
$config['dbpass'] = "xxxxxxxxxxx";                    // Database password
    
$config['tableprefix'] = "";            // Table prefix
    
    
$config['systemtype'] = "drupal";    // valid choices are: drupal, wordpress
    
$config['maxresult'] = 10;                // Number of results to return
    
$config['maxbodylength'] = 60;            // Trimming body length to x number of characters
    
    
    
    
    
if (isset($_REQUEST['s']) && trim($_REQUEST['s'] != ""))
    {
        if (
$link mysql_connect($config['dbhost'], $config['dbusername'], $config['dbpass']))
        {
            if (
mysql_select_db($config['dbname'], $link))
            {
                switch (
strtolower($config['systemtype']))
                {
                    case 
"drupal":
                        
$storytable "torrents";
                        
$titlefield "name";
                        
$bodyfield "genre";
                        
$storyid "id";
                        
$desturl "details.php?id=--storyid--";
                        break;
                    
                    case 
"wordpress":
                        
$storytable "posts";
                        
$titlefield "post_title";
                        
$bodyfield "post_content";
                        
$storyid "ID";
                        
$desturl "?p=--storyid--";
                        break;
                    
                    default:
                        echo 
"Sorry, search is not available.";        // Oopss ..
                
}
                    
                    
                    
                
$stmt "SELECT * FROM " $config['tableprefix'] . "$storytable WHERE $titlefield LIKE '%" addslashes($_REQUEST['s']) . "%' OR $bodyfield LIKE '%" addslashes($_REQUEST['s']) . "%' LIMIT " $config['maxresult'];
                
                if (
$result mysql_query($stmt$link))
                {
                    if (
mysql_num_rows($result) > 0)
                    {
                        while (
$row mysql_fetch_array($resultMYSQL_ASSOC))
                        {
                            
// stripping images and links...
                            
$contents preg_replace("/<img(.)*>|<a (.)*>|<\/a>/i"""$row[$bodyfield]);
                            if (
strlen($contents) > $config['maxbodylength'])
                                
$contents substr($contents0$config['maxbodylength']) . " ...";
                            
$linkurl = (str_replace("--storyid--"$row[$storyid], $desturl));
                            echo 
"<p style=\"cursor: pointer;\" onclick=\"document.location='$linkurl'\"<strong>" $row[$titlefield] . "</strong> - $contents</p>";
                        }  
                    }
                    else
                        echo 
"Sorry, no record matching your search criteria...";
                }
                else
                    echo 
"Sorry, search is not available.";
            }
            else
                echo 
"Sorry, search is not available.";
        }
        else
            echo 
"Sorry, search is not available.";
    }
    else
        echo 
"<script language=\"JavaScript\">HideDiv(\"autocomplete\");</script>";
?>
Attached Thumbnails
search.png  
Reply With Quote
The Following 6 Users Say Thank You to lateam For This Useful Post:
FENIX (26th January 2013), Fynnon (22nd November 2010), Marco (23rd November 2010), mmisu120000 (22nd November 2010), nicukent (27th February 2011), PAX (5th March 2012)