2013-10-23 89 views
0

我的index.php並獲得問題的JSON數組解碼..請幫助我是新來的..JSON解碼PHP的問題

<script> 
$(document).ready(function() { 
    $("#slider_price").slider({ 
     range: true, 
     min: 0, 
     max: 100, 
     step: 1, 
     values: [0, 100], 
     slide: function (event, ui) { 
      $("#app_min_price").text(ui.values[0] + "$"); 
      $("#app_max_price").text(ui.values[1] + "$"); 
     }, 
     stop: function (event, ui) { 
      var nr_total = getresults(ui.values[0], ui.values[1]); 

      $("#results").text(nr_total); 
     }, 
    }); 
    $("#app_min_price").text($("#slider_price").slider("values", 0) + "$"); 
    $("#app_max_price").text($("#slider_price").slider("values", 1) + "$"); 
}); 

function getresults(min_price, max_price) { 
    var number_of_estates = 0; 
    $.ajax({ 
     type: "POST", 
     url: 'search_ajax.php', 
     dataType: 'json', 
     data: { 
      'minprice': min_price, 
      'maxprice': max_price 
     }, 
     async: false, 
     success: function (data) { 
      number_of_estates = data; 
     } 
    }); 
    return number_of_estates; 
} 

而且search_ajax.php

<?php 
require_once('includes/commonFunctions.php'); 
// take the estates from the table named "Estates" 
if(isset($_POST['minprice']) && isset($_POST['maxprice'])) 
{ 
$minprice = filter_var($_POST['minprice'] , FILTER_VALIDATE_INT); 
$maxprice = filter_var($_POST['maxprice'] , FILTER_VALIDATE_INT); 
$query = mysql_query("SELECT * FROM cars WHERE min_range >= $minprice AND max_range  <= $maxprice"); 

$rows = array(); 
while($r = mysql_fetch_assoc($query)) { 
    $rows[] = $r; 
} 

echo json_encode($rows); 

} 
?> 

和問題是我只想在特定的div「number_results」中打印$行。如何解碼該json數組?

+1

請花時間格式化您的代碼,使其至少可讀。通常我會爲你做,但那很糟糕。 –

+0

請給我一些關於這個如何編碼json數組 –

+0

的想法,你必須把'return number_of_estates'裏面的成功 –

回答

0

你確定要傳遞的數據是JSON格式

我覺得應該是

'{"minprice": "min_price", "maxprice":"max_price"}' 
+0

是的,這是最小和最大價格範圍滑塊值..現在它只給我[對象對象],[對象對象],[對象對象] –

+0

@PanditAnkur我想我誤解了你的問題。你想要在成功或行本身顯示行數? – UDB

+0

我需要當滑塊值例如:18或30.輸出將顯示thoes記錄介於18到30之間。 –

0

你不能只從一個函數返回阿賈克斯返回值,因爲AJAX異步是...該函數在ajax調用完成時已經返回number_of_estates

使用回調或只是調用一個函數,並附加您返回的文本有

.. 
stop: function(event, ui) { 
    getresults(ui.values[0], ui.values[1]); 
}, 
... 

function getresults(min_price, max_price) 
{ 
    var number_of_estates = 0; 
    $.ajax({ 
    type: "POST", 
    url: 'search_ajax.php', 
    dataType: 'json', 
    data: {'minprice': min_price, 'maxprice':max_price}, 
    async: false, 
    success: function(data) 
    { 
     number_of_estates = data; 
     $("#results").text(number_of_estates); 
    } 
    }); 
} 

然而AJAX調用每次發生停止funnction所以要小心的時間。

+0

它什麼也沒有顯示.. –

+0

可以發佈你的數據,你得到裏面的成功函數....嘗試'console.log(數據)'並檢查你的開發人員工具 – bipen

+0

與我以前的代碼顯示我[object Object ],[對象對象] #results div。 –