merck_delmoro Posted August 24, 2009 Share Posted August 24, 2009 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; ?> Quote Link to comment Share on other sites More sharing options...
merck_delmoro Posted August 26, 2009 Author Share Posted August 26, 2009 Can Anyone Help Me????????? Quote Link to comment Share on other sites More sharing options...
ignace Posted August 26, 2009 Share Posted August 26, 2009 http://www.phpfreaks.com/forums/index.php/topic,265154.0.html Quote Link to comment Share on other sites More sharing options...
merck_delmoro Posted August 26, 2009 Author Share Posted August 26, 2009 @ignace I dont have problem on searching the multiple words my problem was I want to arrange all the matching strings from my csv file starting from the nearest match string that I typed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.