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;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.