0

I've created a controller & view as follows - I am trying to loop through the jSON object - can anyone assist?

Can anyone demonstrate how to loop through the json object in the view?

//Controller PHP function:
public function ajax_get_all()
{
    $stockists = $this->stockists_model->get_all();
    header('Content-type: application/json');
    echo json_encode($stockists);
}

//HTML View

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function() {
// Stuff to do as soon as the DOM is ready;
$.getJSON("/stockists/ajax_get_all", function(data) {
    var obj = jQuery.parseJSON(data);
    //console.log(obj);
  });
});

1

1 Answer 1

2
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function() {
// Stuff to do as soon as the DOM is ready;
$.getJSON("/stockists/ajax_get_all", function(data) {
    $.each(data, function(key,value){ 
      console.log(key);
      console.log(value);  
    });
    //var obj = jQuery.parseJSON(data);
    //console.log(obj);
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks @Svetlio - simple but effective!! :) will accept shortly

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.