0

I'm trying to make a call to the database and recieve a "toplist" that is limited to 10 in PHP. Here I need to make an array and give it back to the Jquery with $.get().

But it's failing in recieving all the data with this code, How can I make this to be recieved and then do a "for each" in the Jquery for all the data that is being sent back from the PHP?

Jquery:

$.get("core.inc.php?get_toplist=1",
    function(data) {
        var json = JSON.parse(data); 
        alert(json['name']);
});

PHP:

if ($_GET['get_toplist']) {
    $get_top_list = mysql_query("
            SELECT * FROM ".$DBprefix."Submissions 
            WHERE status='1' 
            ORDER BY points DESC LIMIT 10");

    $i = 0;
    $arr = array();
    while ($top_list = mysql_fetch_array($get_top_list)) {   
        $arr[] = array(
            'position' => $i++,
            'name' => $top_list['name'], 
            'points' => $top_list['points']
        ); 
    }

    echo json_encode($arr);

}
5
  • Now I just get "undefined" in the alert() Commented Oct 22, 2013 at 13:27
  • @MichaelBerkowski It's calling json_encode(), so it's returning JSON. The header doesn't have any effect on the data that's returned. Commented Oct 22, 2013 at 13:30
  • In your browser's console, you should at least have seen some output returned from the AJAX request, though it is likely not in the format jQuery is expecting. Commented Oct 22, 2013 at 13:30
  • @MichaelBerkowski No, it doesn't. jQuery just returns the raw data as a string, and JSON.parse() parses it. Commented Oct 22, 2013 at 13:31
  • You get undefined because the data returned is a multi dimensional array and name is inside an inner node Commented Oct 22, 2013 at 13:32

3 Answers 3

2

You're returning an array, but your Javascript code is treating it as a single element. Try:

alert(json[0].name);

If you want a foreach, you can do:

$.each(json, function(i, el) {
    console.log(el.name + " score is " + el.points);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Friend your json will be like .

[{"position":1,"name":"name1","points":"points"},{"position":2,"name":"name2","points":"points"},   {"position":3,"name":"name3","points":"points"},....]

so you can not directly access it like ur way.

make a loop then access it.

$.each(data,function(i,val){
  alert(val.name);
  });

this will work.

Comments

1

your php code is not proper

if ($_GET['get_toplist']) {

          $get_top_list = mysql_query("
          SELECT * FROM ".$DBprefix."Submissions 
          WHERE status='1' 
          ORDER BY points DESC LIMIT 10");

          $i = 0;  
          $arr = array();                                                                                                    
          while ($top_list = mysql_fetch_array($get_top_list)) {   

          $arr[] = array(
                    'position' => $i++,
                    'name' => $top_list['name'], 
                    'points' => $top_list['points']
                    ); 
          }
        }

echo json_encode($arr);

}

just add '}' before echo json_encode($arr);

also you are getting an array change in alert(json[0]['name']);

Comments

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.