2016-03-21 20 views
1

Bu yüzden harici bir JSON dosyasından yenileme gerektirmeden sonuçları görüntüleyen bir giriş alanı oluşturmaya çalışıyorum. Geçerli kodum iyi çalışıyor, ancak doğrudan php dosyasında değil, harici bir JSON dosyasındaki sonuçları nasıl kontrol edebilirim?Php Harici JSON dosyası ile AJAX araması

JSON dosyam: (Sonuçları "ad" dan göstermek istiyorum).

[ 
{"name" : "300", "year" : "1999", "plot" : "X", "run" : "200 min", "rated" : "PG-13", "score" : "10/10", "source" : "A", "id" : "000"}, 
{"name" : "200", "year" : "1999", "plot" : "X", "run" : "200 min", "rated" : "PG-13", "score" : "10/10", "source" : "A", "id" : "000"} 
] 

Benim html ve Javascript:

<script> 
function showHint(str) { 
if (str.length == 0) { 
    document.getElementById("txtHint").innerHTML = ""; 
    return; 
} else { 
    var xmlhttp = new XMLHttpRequest(); 
    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      document.getElementById("txtHint").innerHTML = xmlhttp.responseText; 
     } 
    }; 
    xmlhttp.open("GET", "gethint.php?q=" + str, true); 
    xmlhttp.send(); 
} 
} 
</script> 
<p><b>Start typing a name in the input field below:</b></p> 
<form> 
First name: <input type="text" onkeyup="showHint(this.value)"> 
</form> 
<p>Suggestions: <span id="txtHint"></span></p> 

gethint.php Sadece, file_get_contents kullanarak JSON dosyasının içeriğini almak kullanarak gerçek bir diziye dönüştürmek gerekir

<?php 
//Instead of using these I'd like to use the external JSON file 
$a[] = "Anna"; 
$a[] = "Brittany"; 
$a[] = "Cinderella"; 
$a[] = "Diana"; 
// get the q parameter from URL 
$q = $_REQUEST["q"]; 
$hint = ""; 
// lookup all hints from array if $q is different from "" 
if ($q !== "") { 
$q = strtolower($q); 
$len=strlen($q); 
foreach($a as $name) { 
    if (stristr($q, substr($name, 0, $len))) { 
     if ($hint === "") { 
      $hint = $name; 
     } else { 
      $hint .= ", $name"; 
     } 
    } 
} 
} 
// Output "no suggestion" if no hint was found or output correct values 
echo $hint === "" ? "no suggestion" : $hint; 
?> 
+2

'$ films = json_decode (dosya_get_contents (" your_json_file.json ")) – Chris

+0

$ film değişkenini php koduna nereye koyardım? –

+0

Bunu, $ a' değişkeninizin yerine kullanırsınız, ancak her bir filmin '' a '' öğesinin her birini kullanarak doğrudan yerine '' filmin '' anahtarına erişin. – Chris

cevap

1

json_decode, ve arama sorgusu karşı onları kontrol etmek için her öğenin adını alın:

<?php 

// Here, the TRUE parameter is very important to treat it as an associative array 
$films = json_decode(file_get_contents("path/to/your_json_file.json"), true); 

// get the q parameter from URL if it is set 
$q = isset($_REQUEST["q"]) ? $_REQUEST["q"] : ""; 
$hint = ""; 
// lookup all hints from array 
$q = strtolower($q); 
$len = strlen($q); 
// For each film 
foreach($films as $film) {//          <------ 
    // Get the name 
    $name = $film['name'];//          <------ 
    // If the query is empty or if the query is found 
    if ($q === "" || stristr($q, substr($name, 0, $len))) {// <------ 
     if ($hint === "") { 
      $hint = $name; 
     } else { 
      $hint .= ", $name"; 
     } 
    } 
} 
// Output "no suggestion" if no hint was found or output correct values 
echo $hint === "" ? "no suggestion" : $hint; 


?> 
+0

Arama terimi girilmediyse, tüm önerileri görüntülemenin bir yolu olup olmadığını biliyor musunuz? Çünkü şu anda bir şey arar ve arama terimini siler, öneriler kalır. Bunun yerine tüm önerileri görüntülemek için .. –

+0

@ S.Doe Tabii ki, benim cevabım bunu yapmak için düzenledim (eğer $ q' boşsa, tüm isimleri basacaktır). – blex