0

I am trying to implement AJAX call method into my website using codeigniter, so when the user clicks on a button it will update them live.

The click button works and displays all of the JSON data but the issue is when i try and display a specific array it does not print it out it shows single values for example "h"

I want to be able to print specific arrays for example the array that contains the string "Jamie"

Any help would be appreciated

Controller

public function insertJSON()
    {
        $this->load->model("values");
        $queryresults = $this->values->getDb();

        $arr = array();
        $arr2 = array();


        foreach($queryresults as $row)
        {
            $arr[] =  $row->post;
            $arr2[] = $row->img;
        }

                    $data = array();
                    $data[] = $arr;
                    $data[] = $arr2;

                    echo json_encode($data);
    }

View

<script type='text/javascript' language='javascript'>
  $('#getdata').click(function () {
    $.ajax({
      url: '<?php echo base_url().'index.php/welcome/insertJSON';?>',
      async: false,
      type: "POST",
      success: function(data) {
        $('#result_table').html(data[1]);
      }
    })
  });
</script>

Vardump of the variable

array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(5) "Jamie"
    [1]=>
    string(4) "Mark"
    [2]=>
    string(5) "James"
    [3]=>
    string(5) "hello"
}
[1]=>
  array(4) {
    [0]=>
    string(16) "[email protected]"
    [1]=>
    string(15) "[email protected]"
    [2]=>
    string(15) "[email protected]"
    [3]=>
    string(16) "[email protected]"
}
}
3
  • You have to loop thorough the JSON object Commented Mar 18, 2013 at 13:34
  • data[1] maps to the second array you had added to the $data array. You would need to do data[0][0] to get jamie and data[1][0] to get [email protected] Commented Mar 18, 2013 at 13:35
  • i thought i would have to try 'data[0][0]' to get jamie but instead it displays "<", its really confusing me - i think this suggests that im not grabbing the JSON properly Commented Mar 18, 2013 at 13:36

3 Answers 3

1

if you want display a specific array then use [] operator

$('#result_table').html(data[0][0]); //will print Jamie
$('#result_table').html(data[0][3]); //will print hello
$('#result_table').html(data[1][1]); //will print [email protected]
Sign up to request clarification or add additional context in comments.

Comments

1

Try running this on your returned data:

data = JSON.parse(data);

So:

success: function(data) {
    data = JSON.parse(data);
    $('#result_table').html(data[1]);
}

Comments

0

Some Info: To get jSON correctly you should set output TYPE before putting output like this:

$this->output->set_content_type('application/json');

Then set output like this:

$this->output->set_output(json_encode($data));  

However According to your var dump $data[1] will show Object[] and all the above answer is correct. :)

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.