0

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?

2
  • 2
    All you have to do is pre-process the $arr into a new array in the correct format and then send that back to the browser Commented Nov 29, 2018 at 9:00
  • Don’t use fetchAll, but a while loop that processes one record at a time. Inside that loop, insert the record into an array, using the category value as key. $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.) Commented Nov 29, 2018 at 9:07

2 Answers 2

1

Here is a code to have in a result list of invoices and orders separately. After receiving data on front-end you can use simple code to group all items:

var data = [
   {
      "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"
   }
]

var categories = {
    invoices: [],
    orders: []
}

data.map((item) => {
  if(item['File_category'] === 'order') {
    categories.orders.push(item)
  } else if(item['File_category'] === 'invoice') {
    categories.invoices.push(item)
  }
}) 

console.log(categories)

Than you can just loop over specific categories like categories.invoices or categories.orders and easily append it to body.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer but I have this error Uncaught TypeError: data.map is not a function
See this fiddle: jsfiddle.net/53h9utv6/11 Its a pure js solution which is displaying in console proper values. of course in your code you need to have data object with our JSON values. If you are putting simple JSON. You need to do JSON.parse() on it to be transfered to javascript object.
If one of the solutions is working as expected remember to click +1 and accept correct solution. this will give information to others that problem was solved and have correct answer.
How can I assign a value when it does not exist (for example when there is no invoice) ? For example when it does not exist to assign another value categories['invoices'][0]['file_creation']
1

Doing this using the PHP code:

<?php
//$arrays = $stmt->fetchAll();
$arrays=
[
   [
      "File_name"=>"order_18",
      "File_creation"=>"2018-11-28",
      "File_category"=>"invoice",
   ],
   [
      "File_name"=>"order_18",
      "File_creation"=>"2018-11-25",
      "File_category"=>"order",
   ]
];
foreach($arrays as $index=>$array)
{
    if(isset($array["File_category"]))
    {
        $key=$array["File_category"];
        unset($array["File_category"]);
        $arrays[$key][] = $array;
        unset($arrays[$index]);
    }
}
print_r($arrays);
//echo json_encode($arrays);
?>

The result will be:

Array
(
    [invoice] => Array
        (
            [0] => Array
                (
                    [File_name] => order_18
                    [File_creation] => 2018-11-28
                )
        )
    [order] => Array
        (
            [0] => Array
                (
                    [File_name] => order_18
                    [File_creation] => 2018-11-25
                )
        )
)

1 Comment

I do not understand how it works but it works! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.