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]"
}
}
data[1]maps to the second array you had added to the$dataarray. You would need to dodata[0][0]to getjamieanddata[1][0]to get[email protected]