2013-03-25 160 views
0

我想通過使用select2插件將MySQL遠程數據用作標記。但是我的JSON學習曲線非常薄弱。請幫我修正這個JSON格式。這個JSON爲什麼不起作用?

$("#e7").select2({ 
    placeholder: "Search for a movie", 
    minimumInputLength: 3, 
    ajax: { 
     url: "search.php", 
     dataType: 'jsonp', 
     quietMillis: 100, 
     data: function (term, page) { // page is the one-based page number tracked by Select2 
      return { 
       q: term, //search term 
       page_limit: 10, // page size 
       page: page, // page number 
       apikey: "ju6z9mjyajq2djue3gbvv26t" // please do not use so this example keeps working 
      }; 
     }, 
     results: function (data, page) { 
      var more = (page * 10) < data.total; // whether or not there are more results available 

      // notice we return the value of more so Select2 knows if more results can be loaded 
      return { 
       results: data.movies, 
       more: more 
      }; 
     } 
    }, 
    formatResult: movieFormatResult, // omitted for brevity, see the source of this page 
    formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page 
    dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller 
    escapeMarkup: function (m) { 
     return m; 
    } // we do not want to escape markup since we are displaying html in results 
}); 

這裏是search.php中

<?php 
    $sql=mysqli_query($db3->connection,"SELECT * FROM tags"); 

    while($row=mysqli_fetch_array($sql)){ 
     $tags=$row['tags']; 
     $id=$row['id']; 

     $response=array(); 
     $response['tags']=$tags; 
     $response['id']=$id;  
    } 

    echo json_encode($response); 
?> 
+4

的每一次迭代的$迴應如果不說明是什麼問題,即你希望發生的事情,到底發生了什麼,你等等,那麼它有什麼錯誤信息我們不可能幫助你。請編輯您的問題並詳細說明問題。不,「它不工作」是*不是一個有用的錯誤描述。我的烤麪包機也不工作。 – 2013-03-25 10:27:53

+1

您在while循環中重置$ response。移動'$ response = array();'outside並用'$ response [] = array('tags'=> $ tags,'id'=> $ id)替換內部;'' – Waygood 2013-03-25 10:28:43

回答

0

while循環不填充$迴應陣列。 $響應=陣列();正在重置與循環

$sql=mysqli_query($db3->connection,"SELECT * FROM tags"); 

$response=array(); 
while($row=mysqli_fetch_array($sql)){ 
    $tags=$row['tags']; 
    $id=$row['id']; 

    $response[]=array('tags'=>$tags, 'id'=>$id); 

} 

echo json_encode($response);