views:

72

answers:

6

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.

Alex

A: 

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'

hendepher
+2  A: 

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);
});
Felix Kling
A: 

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);
});
andres descalzo
I don't think `p[0].country` is correct. The return value from PHP is an JSON object with attribute `data` so if any it should be `p.data[0]`. Second you are not generating valid JSON. In JSON, every string has to be inside quotes, so it should be `{"data": [` ...
Felix Kling
-1 You cannot use `getJSON` this way. It is a function making use of Ajax. There is a lot wrong with your code.
Felix Kling
sorry, I was confused
andres descalzo
+1  A: 

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

Davide
A: 
Alex
You should edit your question and not answer to it. How to use `getJSON` is explained in the documentation: http://api.jquery.com/jQuery.getJSON/ BTW the code might work but it seems to be more complicated than it has to be...
Felix Kling
The code you use does not produce valid JSON. Every string has to be inside quotes which `data` is not. What about my or Davide's solution?
Felix Kling
A: 

Felix King thank you very much !! Your code is perfect and i made a mistake yesterday ... I don't how to edit thus i answered another time, but i wanted to thank you

Alex

Alex