Actaully I have to search a input
value from a txt
file, when keydown event
is triggered (it means the search function take place as alphabetically
). That txt
file has multiple lines and keys
.
Now firstly let me describe the whole scenario with appropriate code.
HTML Code:
<input type="text" name="searchval" />
<div></div>
$(document).ready(function(){
$('input').bind('keydown',function(){
setTimeout(search($(this).val().toLowerCase()),2000);
});
});
function search(v){
$.ajax({
url:'search.php',
type:'get',
data:'sv='+v,
dataType:'json',
cache:false,
success:function(r){$('div').empty();for(var i in r){$('div').append(r[i]+",");}},
error:function(a,b,c){$('body').append(b+'<hr/>');}
});
}
PHP Code:
//ADD DATA TO TXT FILE
$file = "UL.txt";
if((!file_exists($file)) || (0 == filesize($file))){
$data = "$name|$email|Offline";
}else{
$data = "\r\n$name|$email|Offline";
}
$fp = fopen("UL.txt", "a") or die("Couldn't open file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
`UL.txt'
Abc Def|abc@def.com|Offline
Ghi Jkl|ghi@jkl.com|Offline
Mno Pqr|mno@pqr.com|Offline
Stu Vwxyz|stuv@wxyz.com|Offline
....
search.php
:
header('Content-Type: application/json');
$search = array();
if($_SERVER["REQUEST_METHOD"] == "GET"){
$searchMe = $_GET['sv'];
$F = file("UL.txt");
foreach($F as $k1 => $v1) {
$d1[$k1] = explode("|", $v1);
$email[] = strtolower(trim($d1[$k1][1])); //$d1[$k1][1];
$name[] = strtolower(trim($d1[$k1][0])); //$d1[$k1][0];
foreach($email as $k2 => $v2){
if($v2 == $searchMe){
$search[] = $v2;
}else{
$search = 'NO RESULT';
}
}
}
}
echo json_encode($search);
Now, this code doesn't work.
Instead of $v2
, $search[]
must return the name:email:status
IF $searchMe
matchs $email
or $name
, but I don't know how to do this. Means how to match words by words
and use the keys
to assign the whole line to the $search[]
.
Thanks & Regards.