PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 19, 2022

[FIXED] how to insert an object into the list in codeigniter

 February 19, 2022     codeigniter, php     No comments   

Issue

from

{
    "status": 1,
    "message": "Success",
    "data": [
        {
            "order": {
                "id_biling": "1",
                "order_id": "A18",
                "kd_bill": "001001202101200001",
                "order_name": "Kita",
                "amount": "20000"
            },
            "detail": [
                {
                    "kd_bill": "001001202101200001",
                    "name": "Odading",
                    "id_item": "66",
                    "amount": "10000",
                    "subtot": "20000",
                    "jml": "2"
                }
            ]
        }
    ]
}

to this

 {
    "status": 1,
    "message": "Success",
    "data": [
        {
            "order": {
                "id_biling": "1",
                "order_id": "A18",
                "kd_bill": "001001202101200001",
                "order_name": "Kita",
                "amount": "20000",
"detail": [
                    {
                        "kd_bill": "001001202101200001",
                        "name": "Odading",
                        "id_item": "66",
                        "amount": "10000",
                        "subtot": "20000",
                        "jml": "2"
                    }
                ]
            }
            
        }
    ]
}

mycode

        $output = [];
        foreach($listorder->result() as $id){

$data['order'] = $this->db->query("SELECT a.id_biling,a.order_id,a.kd_bill,a.order_name,a.amount from pos_biling_kasir a left join pos_ref_tenant b on a.id_tenant=b.id where a.kd_bill='".$id->kd_bill."'")->row();

$data['detail'] = $this->db->query('SELECT *,sum(a.amount) as subtot,count(a.id_item) as jml from (select a.kd_bill,b.name,a.id_item,a.amount from pos_transaction_tenant a left join pos_ref_item b on a.id_item=b.id where a.kd_bill='.$id->kd_bill.' ) a group by a.name')->result();

        array_push($output, $data);
    }

    $jml = $listorder->num_rows();

    
    if($jml > 0){
            $response = [
                    'status' => 1,
                    'message' => 'Success',
                    'data' => $output
                ];
            echo json_encode($response);
        }else{
             $response = [
                    'status' => 0,
                    'message' => 'Failed',
                    'data' => new stdClass()
                    ];
            echo json_encode($response);
        }

Solution

You can do this way to delete detail and push as an element inside order like below-,

$decoded_response = json_decode($response, true);
foreach($decoded_response['data'][0] as $key=>$value){
       if ($key == 'detail'){
           $decoded_response['data'][0]['order'][$key] = $value;
           unset($decoded_response['data'][0][$key]);
       }
}

echo json_encode($decoded_response);

WORKING DEMO: https://3v4l.org/u67kE



Answered By - Always Sunny
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing