Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 返回JSON,使用Javascript编写_Php_Javascript_Ajax_Json - Fatal编程技术网

Php 返回JSON,使用Javascript编写

Php 返回JSON,使用Javascript编写,php,javascript,ajax,json,Php,Javascript,Ajax,Json,嗨 我有个小问题。我试图写一篇ajax文章来从数据库中获取值,它返回的格式是JSON对象。 如何从中获取键和值对 发送ajax的jquery: $ordering = array ("buy_type " . $_POST['buyType'],"prop_type ".$_POST['propertyType'],"district ".$_POST['disctrict'], "street ".$_POST['street'],"room_m

我有个小问题。我试图写一篇ajax文章来从数据库中获取值,它返回的格式是JSON对象。 如何从中获取键和值对

发送ajax的jquery:

$ordering = array ("buy_type " . $_POST['buyType'],"prop_type     ".$_POST['propertyType'],"district ".$_POST['disctrict'],
                    "street ".$_POST['street'],"room_min     ".$_POST['roomMin'],"room_max ".$_POST['roomMax'],
                    "price_min ".$_POST['priceMin'],"price_max     ".$_POST['priceMax'],"condition_type ".$_POST['conditionType'],"heat_type ".$_POST['heatType'],"lift_type ".$_POST['liftType'],"parking_type ".$_POST['parkingType']);

$user=$_SESSION["user"];
$whois = $mysqli->query('SELECT * FROM users WHERE uid='.$mysqli->real_escape_string($user).' ');
$who = $whois->fetch_assoc();

switch($who['user_title']){
    case '0':
        $res=$mysqli->query('SELECT * FROM searches WHERE uid='.$mysqli->real_escape_string($user).' 
                ORDER BY '.$mysqli->real_escape_string($ordering[0]).',
                        '.$mysqli->real_escape_string($ordering[1]).',
                        '.$mysqli->real_escape_string($ordering[2]).',
                        '.$mysqli->real_escape_string($ordering[3]).',
                        '.$mysqli->real_escape_string($ordering[4]).',
                        '.$mysqli->real_escape_string($ordering[5]).',
                        '.$mysqli->real_escape_string($ordering[6]).',
                        '.$mysqli->real_escape_string($ordering[7]).',
                        '.$mysqli->real_escape_string($ordering[8]).',
                        '.$mysqli->real_escape_string($ordering[9]).',
                        '.$mysqli->real_escape_string($ordering[10]).',
                        '.$mysqli->real_escape_string($ordering[11]).'
                  ') or die($mysqli->error);
        while($ki=$res->fetch_assoc()){
            $tomb[] = $ki;
        }
        $tomb = array("query-data"=>$tomb);
        echo json_encode($tomb);
函数getTableData(){

console.log输出为:

Object {query-data: Array[3]}
query-data: Array[3]
0: Object
buy_type: "kiado"
condition_type: "uj"
district: "1"
heat_type: "cirko"
id: "1"
lift_type: "all"
parking_type: "all"
price_max: "22"
price_min: "10"
prop_type: "lakas"
room_max: "3"
room_min: "1"
street: "all"
uid: "3"
__proto__: Object
1: Object
2: Object
length: 3
__proto__: Array[0]
__proto__: Object
php代码选择了正确的内容,并将数据返回到ajax:

$ordering = array ("buy_type " . $_POST['buyType'],"prop_type     ".$_POST['propertyType'],"district ".$_POST['disctrict'],
                    "street ".$_POST['street'],"room_min     ".$_POST['roomMin'],"room_max ".$_POST['roomMax'],
                    "price_min ".$_POST['priceMin'],"price_max     ".$_POST['priceMax'],"condition_type ".$_POST['conditionType'],"heat_type ".$_POST['heatType'],"lift_type ".$_POST['liftType'],"parking_type ".$_POST['parkingType']);

$user=$_SESSION["user"];
$whois = $mysqli->query('SELECT * FROM users WHERE uid='.$mysqli->real_escape_string($user).' ');
$who = $whois->fetch_assoc();

switch($who['user_title']){
    case '0':
        $res=$mysqli->query('SELECT * FROM searches WHERE uid='.$mysqli->real_escape_string($user).' 
                ORDER BY '.$mysqli->real_escape_string($ordering[0]).',
                        '.$mysqli->real_escape_string($ordering[1]).',
                        '.$mysqli->real_escape_string($ordering[2]).',
                        '.$mysqli->real_escape_string($ordering[3]).',
                        '.$mysqli->real_escape_string($ordering[4]).',
                        '.$mysqli->real_escape_string($ordering[5]).',
                        '.$mysqli->real_escape_string($ordering[6]).',
                        '.$mysqli->real_escape_string($ordering[7]).',
                        '.$mysqli->real_escape_string($ordering[8]).',
                        '.$mysqli->real_escape_string($ordering[9]).',
                        '.$mysqli->real_escape_string($ordering[10]).',
                        '.$mysqli->real_escape_string($ordering[11]).'
                  ') or die($mysqli->error);
        while($ki=$res->fetch_assoc()){
            $tomb[] = $ki;
        }
        $tomb = array("query-data"=>$tomb);
        echo json_encode($tomb);

有人能帮我把这些值写到一个表中吗?

你可以通过这样一个对象进行循环:

没有从原型继承属性

for( i in json ) {
    if ( json.hasOwnProperty( i ) ) {
        console.log( i, json[ i ] );
    }
}
for( i in json ) {
    console.log( i, json[ i ] );
}
从原型继承属性

for( i in json ) {
    if ( json.hasOwnProperty( i ) ) {
        console.log( i, json[ i ] );
    }
}
for( i in json ) {
    console.log( i, json[ i ] );
}


i
是键,
json[i]
是值。

您可以这样做

更新:

$.post( 'loader.php', getGetStr(), function( data ) {
  if ( !data || !data['query-data'] ) {
    // invalid json string, so dont process
    return;
  }
  data  = data['query-data'];
  // create the table
  var table = $("<table />").html('<thead></thead><tbody></tbody>');
  // inserted table head cols?
  var thead = false;

  // loop through 'query-data'
  for( i = 0; i < data.length; i++ ) {
    // append 'tr' element to 'tbody'
    var tr1 = $("<tr />").appendTo( table.find("tbody") );
    if ( !thead ) {
      // if not finished creating table head cols, then append 'tr' elemnts to thead
      var tr2 = $("<tr />").appendTo( table.find("thead") );
    }
    // loop if its an object
    if ( typeof data[i] === "object" ) {
      for( j in data[i] ) {
        if ( !thead ) {
          // if not finished creating table head cols, then append 'th' elements to thead
          $("<th />").html( j ).appendTo( tr2 );
        }
        // insert our real dat to table rows
        $("<td />").html( data[i][j] ).appendTo( tr1 );
      }
      // we finished creating table head cols
      thead = true;
    }
  }

  // append the table to whatever element you want,
  // you can also use $("body").html( table );
  table.appendTo( $("body") );
}, "json" );
$.post('loader.php',getstr(),函数(数据){
如果(!data | |!data['query-data'])){
//无效的json字符串,请不要处理
返回;
}
数据=数据['query-data'];
//创建表
变量表=$(“”).html(“”);
//插入式桌头cols?
var thead=假;
//循环“查询数据”
对于(i=0;i
尝试使用console.log(json.query data[0])或foreach迭代查询数据


编写要显示的动态html表格。

以下是打印表格的原则

function printRow(item){
    var html = "<tr>";
    for(var key in item){
        html += "<td>"+key+"</td><td>"+item[key]+"</td">;
    }
    html += "</tr>";
    return html;
}
$.post('loader.php',getGetStr(),function(data){
    var json = $.parseJSON(data);
    console.log(json);
    for(var key in json['query-data']){
        var item = json['query-data'][key];
        var html = "<table>";
        html += printRow(item);
        html += "</table>";
    }
});
函数打印行(项){
var html=“”;
用于(var输入项){

html++++key++++item[key]+“您尝试过什么?它只是一个普通的Javascript对象,您可以使用
json['query-data']
访问它。这是一个可以迭代的数组。只是一个建议,将一个额外的参数传递给
$。post
作为
json
发布,因此您不需要使用
$。parseJSON
ie:$。post(url,params,callback,'json')
;但是输出没有什么不同。我怎样才能得到像key,value这样的值呢?但是我怎样才能从这些查询中写入表呢?不要使用
for..in
来迭代数组。