Good morning, I want to use the Zuck.js library to create stories/statuses like IG/FB/Whatsapp, for this I need to have the following structure:
id: 'vision',
photo: 'logo.jpg',
name: 'nombreUsuario',
link: 'enlace a su perfil',
lastUpdated: 1492665454,
items: [
buildItem('id', 'tipo', duracion, 'ruta', '', false, 1492665454),
buildItem('2', 'photo', 3, 'img/2.jpg', '', '', false, 1492665454),
buildItem('3', 'photo', 3, 'img/3.jpg', '', '', false, 1492665454),
]
I keep the photos in a table that relates each photo that is uploaded with an id of each user. When generating the query and generating a JSON I print them as follows:
[{
"id": "1",
"nick": "milf",
"ruta": "IMG-20160215-WA0017.jpeg",
"fecha": "2019-08-02 03:11:18",
"tipo": "3",
"usuario": "12"
}, {
"id": "5",
"nick": "kata",
"ruta": "IMG-20160409-WA0060.jpg",
"fecha": "2019-08-02 04:15:00",
"tipo": "3",
"usuario": "11"
}, {
"id": "6",
"nick": "milf",
"ruta": "IMG-20160409-WA0062.jpg",
"fecha": "2019-08-02 04:15:00",
"tipo": "3",
"usuario": "12"
}]
The downside is that I print each photo as one line:
And what I need is to show all of all users, example:
Or some other way to print the query to fill the fields from the Zuck.js library
my controller
public function historias(){
$res = $this->Usuarios_model->historias();
foreach ($res->result() as $row) {
$datos[] = array(
'id' => $row->idfotos,
'nick' => $row->nick,
'ruta' => $row->ruta,
'fecha' => $row->fecReg,
'tipo' => $row->tipo_foto,
'usuario' => $row->idusuarios
);
}
echo json_encode($datos);
}
My model
public function historias()
{
$sql = "SELECT usuarios.idusuarios, usuarios.nick, usuarios.fotoPerfil, fotos.idfotos, fotos.fecReg, fotos.tipo_foto, fotos.ruta
FROM `usuarios`
INNER JOIN fotos ON fotos.idusuarios = usuarios.idusuarios
WHERE fotos.tipo_foto = 3";
return $this->db->query($sql);
}
Thanks in advance for reading my question.
To start the query you must group the photos by user, so that you have each one with one or more photos in an array, for this you can use
group by
to group the photos according to theusuarios.idusuarios
and you can usegroup_concat()
which will group each data of the columns separated by ",".In the model
Then, in the controller, you must obtain the data from the photographs and place them in an array that will be
$items
, for this, for each user, divide each column of the photograph withexplode(',',$columna)
, which results in arrays with which the 'items' field will be filled, which when final is sent in the array of$datos
:in controller
Finally, the result of the json would be as follows:
I hope it is useful to you, regards.