In AJAX I retrieve in database the last date of a document by category (for example the category invoice, orders, etc.) thanks to this request:
// In each category there can be several documents
$stmt = $bdd->prepare('SELECT file_name, max(file_creation) as file_creation, file_category
FROM all_files
WHERE ...
GROUP BY file_category');
$stmt ->execute(array(
...
));
$arr = $stmt->fetchAll();
echo json_encode($arr);
So I get it back in JSON:
[
{
"File_name": "order_18"
"0": "order_18"
"File_creation": "2018-11-28"
"1": "2018-11-28"
"File_category": invoice "
"3": invoice "
}
{
"File_name": "order_18"
"0": "order_18"
"File_creation": "2018-11-25"
"1": "2018-11-25"
"File_category": "order"
"3": "order"
}
]
I then want to place each data in the right places with jquery, like this:
$ ('#label-order').text('') // the text will have to be: 2018-11-25
$ ('#label-invoice').text('') // the text will have to be: 2018-11-28
The problem is that I do not know how to recover the data that interests me to put it in the right places because the number of categories will increase over time
So I thought of doing something like that, to get the data back to data ["invoice"] ["file_creation"] and data ["order"] ["file_creation"]:
[
"invoice": {
"File_name": "order_18"
"0": "order_18"
"File_creation": "2018-11-28"
"1": "2018-11-28"
"File_category": invoice "
"3": invoice "
}
"order": {
"File_name": "order_18"
"0": "order_18"
"File_creation": "2018-11-25"
"1": "2018-11-25"
"File_category": "order"
"3": "order"
}
]
Is that possible? If so, how can I do? Is there a better solution?
$arrinto a new array in the correct format and then send that back to the browser$data[$row['File_category']] = $row;(Also, you should use fetch mode assoc, not both - right now you have each value in there twice, which you probably don’t really need.)