Wednesday 18 December 2013

Iterate through multiple rows in jquery using ajax, json, and php

How to iterate through multiple rows in jquery using ajax, json, and php
=================================================
REF LINK :
http://stackoverflow.com/questions/8449860/how-to-iterate-through-multiple-rows-in-jquery-using-ajax-json-and-php


I have a php page that returns json like this:

while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
        $rows[] = $row;
}
print json_encode($rows);

When I run the php page I get back something like:

[{"phone":"1234567"},{"phone":"1234567"}]

 $.ajax({
        url: 'test.php',
        type: 'POST',
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(response) {

             $.each(response, function() {
                $.each(this, function(key, value){
                    alert(key + " --> " + value);
                });
            });

        }
   });

});

I got that from another SO question. This will give me my keys and value in the alert. This was just for me to make sure everything works. I have searched but cannot figure out how to just get one value. Say I wanted the name, what do I put? I have tried:

success: function(response) {
 var obj = $.parseJSON(response);
 alert( obj.phone );
}

But since it is multiple rows it won't work unless I have, and it also fails when I have one row like this:

echo json_encode(array("phone" => "123")


3 Answers
============

The response variable is an array of objects. To access a single index in your response variable:

var phone = response[0].phone;//replace `0` with the index you want
If you end-up iterating through your response in a loop make sure to do it like this for the best performance:

var len = response.length;
for (var i = 0; i < len; i++) {
    console.log(response[i].phone);
}
Check-out this jsperf to see how much faster this type of loop is than for(i in array): http://jsperf.com/for-in-tests

6 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    Best digital marketing training Institute in chennai

    ReplyDelete
  3. event manager Images on landing pages, widgets on event app home pages, banner ads in speaker listings, and the like have been around for 15 years, and they’re not going anywhere. work ethic definition, webinar is and meeting invite sample

    ReplyDelete
  4. Very useful and informative post. I really enjoyed reading this article and will surely visit your website again.
    dot-net-full-stack-developer-training-in-hyderabad

    ReplyDelete