2017-09-15 82 views
1

當一條記錄顯示數據時多條記錄來不顯示數據的其他網站。 ajaxx.phpajax無法正常顯示php數據

<?php 
    include 'database.php'; 
    session_start(); 

    $post = $_POST; 


     $search = $post['search']; 
     $searchType = $post['searchType']; 

     if ($searchType == 'all') 
       {$sql = "SELECT DISTINCT title FROM hadees WHERE title LIKE '$search%' AND (type='Bukhari' OR type='Muslim') ";} 
      else 
       {$sql = "SELECT DISTINCT title FROM hadees WHERE title LIKE '$search%' AND type='$searchType' ";} 

     $result = mysqli_query($db,$sql); 

     if ($result->num_rows > 0) { 
      while($row = $result->fetch_assoc()) { 
       $row['title']; 
       echo json_encode($row); 
      } 
     } else 
      { echo "Not Found Result" ; } 


?> 

當數據記錄是一個然後將數據追加成功地在多個記錄來那麼不顯示數據,並追加不行

JavaScript代碼

function searchh() 
{ 
    var type = $("input[name='type']:checked").val(); 
    var searchhh = $(".myButton option:selected").text(); 
    debugger; 
    $.ajax({ 
        url: 'ajaxx.php', 
        type: "POST", 
        data: {'searchType':type, 'search':searchhh}, 
        success: function (data) { 
         var duce = jQuery.parseJSON(data); 
         alert(duce.title); 
        } 
       }); 
} 

回答

1

我覺得你的問題在while循環中。你不想逐一編碼每一行,但總體來說就是這樣。

 $myResults = []; 
     while($row = $result->fetch_assoc()) { 
      $row['title']; 
      $myResults[] = $row; 
     } 
     echo json_encode($myResults); 
0

通過在循環中使用echo json_encode($row);可以生成無效的JSON。

嘗試製作一個行數組,然後顯示它。

if($result->num_rows > 0) 
{ 
    $output = array(); 
    while($row = $result->fetch_assoc()) 
    { 
     output[] = $row; 
    } 
    if($searchType == 'all') 
    { 
     echo json_encode($output); 
    } 
    else 
    { 
     echo json_encode(current($output)); // print just one 
    } 
}