开发者

MYSQL Data -> PHP arrays -> Javascript (or Jquery) How can I pass the data with JSON?

I am starting with a new site (it's my first one) and I am getting big troubles ! I wrote this code

<?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;
    }
?>

and the output is as follows :

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

which should be a correct JSON outout ...

How can I get it now in Jquery ? I tried with

$.getJSON 

but I am not able to fuse it properly. I don't want yet to pass the data to a DIV or something similar in HTML.

As an update, the code of Andres Descalzo works !

<?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 "]}";
?>

The output is correct and is as follows :

{data: [{"country":"liechtenstein"},{"country":"romania"},{"country":"jugoslavia"},{"country":"polonia"}]}
开发者_开发百科

How can I use $getJSON ?

It's ok that the syntax is

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

and that the url is the above mentioned PHP file but [data] and callback function?


It is not correct JSON. Correct would be, if the elements were enclosed in square brackets (indicating an array) like so:

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

You can first fetch all elements from the DB in an array and then encode this array:

$elements = array()

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

echo json_encode($elements);

This should work (using $.getJSON() in jQuery).

Update: A .getJSON() example:

$.getJSON('/path/to/php_file', function(data) {
    // something with data which is of form
    // data = [{'country': '...'}, {...}, ...]
    //e.g.
    alert(data[0].country);
});


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 :)


I think you want to echo once - after the loop. Also, if you want to pass it as an array, surround it with brackets. Something like this:

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

The result you would be sending would be:

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

As to $.getJSON, how are you applying this? The syntax for getJSON is:

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

You need a callback function to make use of 'data'


you can try this way:

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);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜