1
Guys, I need some help setting up a bad array here. I got a calendar (bootstrap Calendar) ready and I’m trying to implement it on my site with Cakephp. All right with the layout but when searching for the information in the bank he expects to receive an Array as follows:
{"success":1,"result":[{"0":"1","id":"1","1":"Teste","title":"Teste","2":"Apenas para Teste","body":"Apenas para Teste","3":".\/descripcion_evento.php?id=1","url":".\/descripcion_evento.php?id=1","4":"event-important","class":"event-important","5":"1443105000000","start":"1443105000000","6":"1443112800000","end":"1443112800000"}]}
In the original vault he rides as follows:
if ($conexion->query($sql)->num_rows) {
$datos = array();
$i=0;
$e = $conexion->query($sql);
while($row=$e->fetch_array())
{
$datos[$i] = $row;
$i++;
}
echo json_encode(
array(
"success" => 1,
"result" => $datos
)
);
}
But there’s no way I can do that for cakephp. The best result I’ve had is this:
{"success":1,"result":[{"Agenda":{"id":"1","title":"teste","body":"teste","url":"teste","class":"event-important","start":"1443105000000","end":"1443112800000","0":"1","1":"teste","2":"teste","3":"teste","4":"event-important","5":"1443105000000","6":"1443112800000"}}]}
My controller was like this:
public function obter_eventos(){
$datos = $this->Agenda->find('all');
foreach ($datos as $key => $dato) {
foreach ($dato as $value) {
$i = 0;
foreach ($value as $valor) {
$datos[$key]['Agenda'][$i] = $valor;
$i++;
}
}
}
echo json_encode(
array(
"success" => 1,
"result" => $datos
)
);
$this->autoRender=false;
}
I appreciate the help.
You can post the result of a
var_dump
of the variable$datos
?– KaduAmaral
From Model: array (size=1) 0 => array (size=1) 'Agenda' => array (size=7) 'id' => string '1' (length=1) 'title' => string 'test' (length=5) 'body' => string 'test' (length=5) 'url' => 'test' string(length=5) 'class' => string 'Event-Important' (length=15) 'start' => string '1443105000000' (length=13) 'end' => string '1443112800000' (length=13)
– Rodrigo Gaster
After Model : array (size=1) 0 => array (size=1) 'Agenda' => array (size=7) 'id' => string '1' (length=1) 'title' => string 'test' (length=5) 'body' => string 'test' (length=5) 'url' => 'test' string(length=5) 'class' => string 'Event-Important' (length=15) 'start' => string '1443105000000' (length=13) 'end' => string '1443112800000' (length=13)
– Rodrigo Gaster
Rodrigo, get this one
var_dump
when more than one query log comes? Take it right after the query, after the line$datos = $this->Agenda->find('all');
does thevar_dump
of the result.– KaduAmaral
It normally mounts the 2 array, not to put here because it exceeds the character size.
– Rodrigo Gaster
Rodrigo on the line
"result" => $datos
exchange for"result" => $datos['Agenda']
. I think it already solves your problem, if it works out, let me know to publish as answer.– KaduAmaral
Ask the question, just click on edit just below it. But do this test I said before...
– KaduAmaral
Strange it returned null {"Success":1,"result":null}
– Rodrigo Gaster
Either you changed something, or you passed me one
var_dump
different from what you have in the variable.– KaduAmaral