Jump to content

can anyone help me on this script search multiple words


merck_delmoro

Recommended Posts

I have made a code that search multiple words from string and I have made it but there is one thing missing on my code I want the the list of search found will the sort into the nearest match string

 

here's my code

 

index.html

<html>
<head>
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>

<form>
First Name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <br><span id="txtHint"></span></p>
</body>
</html>

 

javascript.js

var xmlhttp

function showHint(str)
{
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support XMLHTTP!");
  return;
  }
var url="getmatches.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

getmatches.php

<?php
$count = 0;
$matches = array();
$handle = fopen("data.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);

    for ($c=0; $c < $num; $c++) {
        $a[$count][$c] = $data[$c];
$names[$count][$c] = $data[$c];
    }
$count++;
}
fclose($handle);
//get the q parameter from URL
$q=$_GET["q"];
$find = explode(" ",$q);
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
  {
  $hint="";
  for($count=0;$count<count($a);$count++)
  {
   for($i=0; $i<count($a); $i++)
     {

      if (preg_match('/\b(' .implode('|', $find) .')/i', $a[$count][$i],$match))
       {
      	if ($hint=="")
        {
        	$hint=$a[$count][$i];
        }
      	else
        {
        	$hint=$hint."<br> ".$names[$count][$i];
        }
       }
     }
   }
  }

// Set output to "no suggestion" if no hint were found
// or to the correct values

if ($hint == "")
  {
  $response="no suggestion";
  }
else
  {
  $response=$hint;
  }

//output the response
echo $response;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.