0

Okay i change the script like this ! why it dont work ?!?

demo.php

while($row = mysql_fetch_array($result)){   
    $array[] = array(
        'var1' => $row['var1'],
        'var2' => $row['var2'],
        'var3' => $row['var3'],
        'var4' => $row['var4'],
    );
}
print json_encode($array);

demo.js

$.getJSON("demo.php",function(result){
    $.each(result, function(i, obj){
    $(obj.var1).appendTo('div id="var1"');
    $(obj.var2).appendTo('div id="var2"');
    $(obj.var3).appendTo('div id="var3"');
    $(obj.var4).appendTo('div id="var4"');
});
4
  • 2
    what do mean by learn any single attribute in my html page Commented May 8, 2012 at 11:55
  • Why do you quote your variables? That makes no sense. Commented May 8, 2012 at 12:04
  • I hope this can help you api.jquery.com/jQuery.getJSON Commented May 8, 2012 at 12:07
  • i need to set lat lgn fo my marker map take from my database ! Commented May 8, 2012 at 15:25

1 Answer 1

2

Firstly, the PHP:

As previously mentioned, you don't need quotes (") around the variables in your database connection, it's pointless.

You also need to get all the rows before JSON encoding. Change:

echo json_encode(mysql_fetch_assoc($result));

To

$return = array();
while ($row = mysql_fetch_assoc($result))
{
    $return[] = $row;
}
echo json_encode($return);

So that all the rows from the database are returned, instead of just the first.

Next for your jQuery:

Try changing: $('div').html(obj.attribute1+" "+obj.attribute2 ecc...);

To: $(obj.attribute1+" "+obj.attribute2 ecc...).appendTo('div');

Or: $('<div>' + obj.attribute1+" "+obj.attribute2 ecc... + '</div>').appendTo('body'); if you want each object in it's own container.

The way you are currently doing it will just overwrite the HTML for every div on the page for each iteration so that div elements will eventually only contain details for the last object in the collection.

Edit:

$.getJSON("demo.php",function(result){
    var $id = 0;
    $.each(result, function(i, obj){
        $id ++;
        $('<div id="obj' + $id + '"/>').appendTo('body');
        $('<span class="var1">' + obj.var1 + '</span>').appendTo('div#obj' + $id);
        $('<span class="var2">' + obj.var2 + '</span>').appendTo('div#obj' + $id);
        $('<span class="var3">' + obj.var3 + '</span>').appendTo('div#obj' + $id);
        $('<span class="var4">' + obj.var4 + '</span>').appendTo('div#obj' + $id);
    });
});

Edit 2:

If you are trying to get each of the 'varX' from all elements into a single div, then just define the four divs (<div id="var1"/>, <div id="var2"/>, ...), and use this code:

$.getJSON("demo.php",function(result){
    $.each(result, function(i, obj){
        $(obj.var1).appendTo('div#var1');
        $(obj.var2).appendTo('div#var2');
        $(obj.var3).appendTo('div#var3');
        $(obj.var4).appendTo('div#var4');
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Check edit for new solution. The reason it doesn't work is because you are: 1. Attempting to reference the div using invalid syntax. 2. Attempting to reference divs before they exist. 3. You missed the enclosure ending for you $.each.
@KristianWilliams To get your profiles merged, please edit each one to say "merge me" to confirm your ownership and then contact us. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.