简体   繁体   中英

How to get JSON data from database groupby Date in Codeigniter

I have a table and I want to get data from database and return it in JSON fromat date vise, below is my table:

id  userId      Date      Time   record
1     1      15-Oct-2017  3:50    152
2     1      15-Oct-2017  4:30    142
3     1      16-Oct-2017  8:50    130
4     2      15-Oct-2017  2:00    90
5     2      15-Oct-2017  4:50    154
6     2      15-Oct-2017  5:00    120

I created a function in which I called the data from database and return the output in JSON fromat

public function getRecord()
    {
        $userId = $this->input->get('userId');
        $this->db->from('xyz');
        $this->db->where('userId',$userId);
        $record = $this->db->get()->result();

        return $this->output->set_output(json_encode(array(     
                'status' => 'Ok',
                'statusCode' =>'801',
                'response' => $record

            )));
    }

it returns me something like this, which is not required by me (I know I didn't do it in right way)

{
  "status": "Ok",
  "statusCode": "801",
  "response": Array[3][
    {
      "id": "1",
      "userId": "1",
      "date": "15-Oct-2017",
      "time": "3:50",
      "record": "152"
    },
    {
      "id": "2",
      "userId": "1",
      "date": "15-Oct-2017",
      "time": "4:30",
      "record": "142"
    },
    {
      "id": "3",
      "userId": "1",
      "date": "16-Oct-2017",
      "time": "8:50",
      "record": "130"
    }
  ]
}

But I want some thing like this, the output will be putted in date vise

{
"status": "Ok",
"statusCode": "801",
"response": Array[3][

[
"date": "15-Oct-2017"
{ 
"id": "1",
"userId": "1",
"date": "15-Oct-2017",
"time": "3:50",
"record": "152"
},
{  
"id": "2",
"userId": "1",
"date": "15-Oct-2017",
"time": "4:30",
"record": "142"
}
],

[
"date": "16-Oct-2017"
{
"id": "3",
"userId": "1",
"date": "16-Oct-2017",
"time": "8:50",
"record": "130"
}
]

]
}

Although the result json that you presented seems to no be valid, and since, I think you want the records grouped by date, try looping through the records array grouping them on another array by the date:

<?php

    public function getRecord()
    {
        $userId = $this->input->get('userId');
        $this->db->from('xyz');
        $this->db->where('userId',$userId);
        $record = $this->db->get()->result();
        $orderedRecord = [];

        foreach ($record as $r) {
            if (!isset($orderedRecord[$r->Date])) {
                $orderedRecord[$r->Date] = [];
            }

            $orderedRecord[$r->Date][] = $r;
        }

        return $this->output->set_output(json_encode(array(     
            'status' => 'Ok',
            'statusCode' =>'801',
            'response' => $orderedRecord
        )));
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM