1

我从一个新站点开始(这是我的第一个站点),但遇到了大麻烦!我写了这段代码

<?php
    include("misc.inc");
    $cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
    $query="SELECT DISTINCT country FROM stamps";
    $result=mysqli_query($cxn,$query) or die ("couldn't execute query");
    $numberOfRows=mysqli_num_rows($result);

    for ($i=0;$i<$numberOfRows;$i++){
        $row=mysqli_fetch_assoc($result);
        extract($row);
        $a=json_encode($row);
        $a=$a.",";
        echo $a;
    }
?>

输出如下:

{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"},

这应该是一个正确的 JSON 输出...

我现在如何在 Jquery 中获得它?我试过了

$.getJSON 

但我无法正确融合它。我还不想将数据传递给 DIV 或 HTML 中的类似内容。

作为更新,Andres Descalzo 的代码有效!

<?php
    include("misc.inc");
    $cxn=mysqli_connect($host,$user,$password,$database) or die("couldn't connect to server");
    $query="SELECT DISTINCT country FROM stamps";
    $result=mysqli_query($cxn,$query) or die ("couldn't execute query");
    $numberOfRows=mysqli_num_rows($result);

    echo "{data: [";
    for ($i=0; $i<$numberOfRows; $i++){
        $row=mysqli_fetch_assoc($result);
        extract($row);
        $a = (($i!=0)?",":"") . json_encode($row);
        echo $a;
    }
    echo "]}";
?>

输出是正确的,如下所示:

{data: [{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"}]}

我该如何使用$getJSON

没关系,语法是

$.getJSON( url, [ data ], [ callback(data, textStatus) ] )

并且url是上面提到的PHP文件但是[data]和回调函数?

4

4 回答 4

3

它不是正确的 JSON。正确的是,如果将元素括在方括号中(表示数组),如下所示:

[{"country":"liechtenstein"},
 {"country":"romania"},
 {"country":"jugoslavia"},
 {"country":"polonia"}]

您可以首先从数据库中获取数组中的所有元素,然后对该数组进行编码:

$elements = array()

for ($i=0;$i<$numberOfRows;$i++){
        $elements[]=mysqli_fetch_assoc($result);
}

echo json_encode($elements);

这应该可以工作($.getJSON()在 jQuery 中使用)。

更新:一个.getJSON()例子:

$.getJSON('/path/to/php_file', function(data) {
    // something with data which is of form
    // data = [{'country': '...'}, {...}, ...]
    //e.g.
    alert(data[0].country);
});
于 2010-04-29T16:56:46.347 回答
1

Pay attention, the JSON string is not a valid JSON string! I suggest you to use json_encode once, just before producing the output. You'd probably do that:

$countries = array();
for ($i=0;$i<$numberOfRows;$i++){
    $row=mysqli_fetch_assoc($result);

    //Not needed, I guess
    //extract($row); 

    $countries[] = $row;

    //More probably, you want to get only the country name
    //$countries[] = $row['country'];
}

$result = json_encode( $countries );
echo $result;

Hope it's correct, I haven't tested it :)

于 2010-04-29T17:04:49.757 回答
0

我想你想回声一次 - 在循环之后。此外,如果要将其作为数组传递,请用括号括起来。像这样的东西:

for ($i=0;$i<$numberOfRows;$i++){
        $row=mysqli_fetch_assoc($result);
        extract($row);
        $a=json_encode($row);
        $a=$a.",";
    }
echo '['.$a.']';

您将发送的结果是:

[{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"},]

至于 $.getJSON,你是如何应用它的?getJSON 的语法是:

$.getJSON( url, [ data ], [ callback(data, textStatus) ] )

您需要一个回调函数来使用“数据”

于 2010-04-29T16:56:15.457 回答
0

你可以这样尝试:

PHP/HTML:

<div id="iddiv">
<?php
    echo "{data: [";
    for ($i=0; $i<$numberOfRows; $i++){
        $row=mysqli_fetch_assoc($result);
        extract($row);
        $a = (($i!=0)?",":"") . json_encode($row);
        echo $a;
    }
    echo "]}";
?>
</div>

Javascript:

$(function(){    
    var p = $.getJSON($("#iddiv").text());
    alert(p[0].country);
});
于 2010-04-29T16:56:55.283 回答