How Is Output(Echo) Array Without Use of JSON_Encode(Codeigniter)

how is output(echo) array without use of json_encode?(codeigniter)

If you just want to view the array, print_r($array) or var_dump($array) will work

how to get element from json_encode array response - Codeigniter

You need to convert json to object, and after that you can get the element:

$respObject = json_decode($response);
echo $respObject->item;
exit;

how to json encode correctly in codeigniter

You firstly need to move

$json = json_encode($cabang, JSON_FORCE_OBJECT); 
echo $json;

outside your foreach loop. At the moment it's executing that command every time your loop runs, so you're outputting the $cabang array multiple times, and each time it contains more data, as the result of the latest loop is added.

You can also remove the JSON_FORCE_OBJECT setting if you want an array as the output, as per your question. And you need to make $cabang a multi-dimensional array.

$cabang = array();

foreach($listcoa->result() as $list){
$item = array();
$item[0] = $list->no_account;
$item[1] = $list->deskripsi_coa;
$item[2] = $list->tipe_akun;
$cabang[] = $item;
}

$json = json_encode($cabang);
echo $json;

Using Exit in CodeIgniter output makes the content type header to text/html instead of the defined one

It is because you are using directly the echo.

Instead use the set_output. Docs here

public function jsonExit($array)
{
$this->con->output->set_content_type('application/json'); // $this->con is get_instance from the constructor
$this->con->output->set_output(json_encode($array));
}

If you need the exitor dieuse _display. Docs here

This method is called automatically at the end of script execution, you won’t need to call it manually unless you are aborting script execution using exit() or die() in your code.

public function jsonExit($array)
{
$this->con->output->set_content_type('application/json'); // $this->con is get_instance from the constructor
$this->con->output->_display(json_encode($array));
exit(0);
}

Or as it is used in the example

Print json_encode object in Codeigniter View returned from controller

OK, so you're getting results in json format. You just need to manipulate those results in your ajax return handler and then append them to the div:

$.get("<?php echo $base_url;?>" + "controller/get_results/" + loaded_messages,  
function(data){
var data = JSON.parse(data);
var output = "";
for (var i = 0; i < data.length; i++) {
output += data[i].email + ' - <br />';
}
$("#main_content").append(output);
}
);

variable undefined when get data from json_encode (Function PHP)

If you are just sending a string (the output from number_format), you don't need to json_encode it.

PHP

public function convertString($value){
echo number_format($value,2,',','.'); // Echo directly
die(); // End script
}

JS

var sisa = (persentasevco / 100) * (jumlahvco)
document.getElementById('periodetk').value = periodetk
var totalt = sisa * periodet
$.ajax({
url: '<?=base_url(\'lahan/convertString/\')?>' + totalt,
success: function (data) {
document.getElementById('persediaanvco').value = data
},
})


Related Topics



Leave a reply



Submit