Home > Mobile >  Having Issues With foreach loop
Having Issues With foreach loop

Time:06-18

I am looping through an array of transactions and then get the items ordered and loop through it to get the item categories and the amount but when I print I discover the second order has the items in the first other and the third one has that of the second order please help

$result = $query_stmt->fetchAll(PDO::FETCH_ASSOC);
                    
// print_r($items);
foreach($result as $key => $value){
   
    $items = json_decode($value["items"],true);                           

    foreach ($items as $key1 => $value1) {
                                   
    $pair_id =  $value1['pair_id'];
    $qty = $value1['qty'];

    $stmt = "SELECT price,cloth_id as cloth FROM pricing WHERE `serial` = :serial";
    $qry_stmt = $this->conn->prepare($stmt);
    $qry_stmt->bindValue(':serial', $pair_id, PDO::PARAM_INT);
    $qry_stmt->execute();
    $price = $qry_stmt->fetch(PDO::FETCH_ASSOC);

    $cloth_type = $price['cloth'];
    $quantity = $qty;
    $amount = $price['price'];
    $total_price = (int)$amount * (int)$qty;
    
    $value2['cloth'] = $cloth_type;
    $value2['quantity'] = $quantity;
    $value2['amount'] = (int)$amount;
    $value2['total_price'] = $total_price;

    $done[] = $value2;
    
}
    $value['items'] = $done;
    $value['order_amount'] = (int)$value['order_amount'];
    $resp[] =$value;
}
    
 

return [
    "success" => 1,
    "data" =>$resp
];

Here is the response I get

{
    "success": 1,
    "data": [
        {
            "order_id": "34",
            "order_ref": "DASYXKGD4N",
            "items": [
                {
                    "cloth": "jeans",
                    "quantity": 10,
                    "amount": 500,
                    "total_price": 5000
                },
                {
                    "cloth": "shirt",
                    "quantity": 2,
                    "amount": 500,
                    "total_price": 1000
                },
                {
                    "cloth": "suit",
                    "quantity": 5,
                    "amount": 5000,
                    "total_price": 25000
                }
            ],
            "order_amount": 32200,
            "date": "2022-06-13 12:52:20",
            "payment_method": "wallet",
            "payment_status": "successful",
            "branch_address": "Calabar Branch",
            "branch_phone": "08074224016",
            "branch_email": "[email protected]",
            "service_name": "Laundry",
            "pickup_address": "No. 30 ofem Ntum Street",
            "pickup_time": "0000-00-00 00:00:00",
            "dropoff_address": "No. 30 ofem Ntum Street",
            "dropoff_time": "0000-00-00 00:00:00",
            "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
            "delivery_status": "PROCCESSING",
            "delivery_method": "Pick-up & Drop-off",
            "delivery_price": "400"
        },
        {
            "order_id": "37",
            "order_ref": "DLJLGD80MT",
            "items": [
                {
                    "cloth": "jeans",
                    "quantity": 10,
                    "amount": 500,
                    "total_price": 5000
                },
                {
                    "cloth": "shirt",
                    "quantity": 2,
                    "amount": 500,
                    "total_price": 1000
                },
                {
                    "cloth": "suit",
                    "quantity": 5,
                    "amount": 5000,
                    "total_price": 25000
                },
                {
                    "cloth": "jeans",
                    "quantity": 10,
                    "amount": 500,
                    "total_price": 5000
                },
                {
                    "cloth": "jackets",
                    "quantity": 2,
                    "amount": 700,
                    "total_price": 1400
                },
                {
                    "cloth": "shirt",
                    "quantity": 2,
                    "amount": 500,
                    "total_price": 1000
                },
                {
                    "cloth": "suit",
                    "quantity": 5,
                    "amount": 5000,
                    "total_price": 25000
                }
            ],
            "order_amount": 32400,
            "date": "2022-06-17 18:26:07",
            "payment_method": "wallet",
            "payment_status": "successful",
            "branch_address": "Calabar Branch",
            "branch_phone": "08074224016",
            "branch_email": "[email protected]",
            "service_name": "Laundry",
            "pickup_address": "No. 30 ofem Ntum Street",
            "pickup_time": "0000-00-00 00:00:00",
            "dropoff_address": "No. 30 ofem Ntum Street",
            "dropoff_time": "0000-00-00 00:00:00",
            "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
            "delivery_status": "PROCCESSING",
            "delivery_method": "Pick-up & Drop-off",
            "delivery_price": "400"
        },
        {
            "order_id": "38",
            "order_ref": "FAPFCXDV8W",
            "items": [
                {
                    "cloth": "jeans",
                    "quantity": 10,
                    "amount": 500,
                    "total_price": 5000
                },
                {
                    "cloth": "shirt",
                    "quantity": 2,
                    "amount": 500,
                    "total_price": 1000
                },
                {
                    "cloth": "suit",
                    "quantity": 5,
                    "amount": 5000,
                    "total_price": 25000
                },
                {
                    "cloth": "jeans",
                    "quantity": 10,
                    "amount": 500,
                    "total_price": 5000
                },
                {
                    "cloth": "jackets",
                    "quantity": 2,
                    "amount": 700,
                    "total_price": 1400
                },
                {
                    "cloth": "shirt",
                    "quantity": 2,
                    "amount": 500,
                    "total_price": 1000
                },
                {
                    "cloth": "suit",
                    "quantity": 5,
                    "amount": 5000,
                    "total_price": 25000
                },
                {
                    "cloth": "jeans",
                    "quantity": 1,
                    "amount": 500,
                    "total_price": 500
                },
                {
                    "cloth": "jackets",
                    "quantity": 1,
                    "amount": 700,
                    "total_price": 700
                },
                {
                    "cloth": "shirt",
                    "quantity": 1,
                    "amount": 500,
                    "total_price": 500
                },
                {
                    "cloth": "suit",
                    "quantity": 1,
                    "amount": 5000,
                    "total_price": 5000
                }
            ],
            "order_amount": 6700,
            "date": "2022-06-18 10:55:25",
            "payment_method": "wallet",
            "payment_status": "successful",
            "branch_address": "Calabar Branch",
            "branch_phone": "08074224016",
            "branch_email": "[email protected]",
            "service_name": "Laundry",
            "pickup_address": "No. 30 ofem Ntum Street",
            "pickup_time": "0000-00-00 00:00:00",
            "dropoff_address": "No. 30 ofem Ntum Street",
            "dropoff_time": "0000-00-00 00:00:00",
            "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
            "delivery_status": "PROCCESSING",
            "delivery_method": "Pick-up & Drop-off",
            "delivery_price": "400"
        },
        {
            "order_id": "39",
            "order_ref": "BHWMSLAR1E",
            "items": [
                {
                    "cloth": "jeans",
                    "quantity": 10,
                    "amount": 500,
                    "total_price": 5000
                },
                {
                    "cloth": "shirt",
                    "quantity": 2,
                    "amount": 500,
                    "total_price": 1000
                },
                {
                    "cloth": "suit",
                    "quantity": 5,
                    "amount": 5000,
                    "total_price": 25000
                },
                {
                    "cloth": "jeans",
                    "quantity": 10,
                    "amount": 500,
                    "total_price": 5000
                },
                {
                    "cloth": "jackets",
                    "quantity": 2,
                    "amount": 700,
                    "total_price": 1400
                },
                {
                    "cloth": "shirt",
                    "quantity": 2,
                    "amount": 500,
                    "total_price": 1000
                },
                {
                    "cloth": "suit",
                    "quantity": 5,
                    "amount": 5000,
                    "total_price": 25000
                },
                {
                    "cloth": "jeans",
                    "quantity": 1,
                    "amount": 500,
                    "total_price": 500
                },
                {
                    "cloth": "jackets",
                    "quantity": 1,
                    "amount": 700,
                    "total_price": 700
                },
                {
                    "cloth": "shirt",
                    "quantity": 1,
                    "amount": 500,
                    "total_price": 500
                },
                {
                    "cloth": "suit",
                    "quantity": 1,
                    "amount": 5000,
                    "total_price": 5000
                },
                {
                    "cloth": "jeans",
                    "quantity": 1,
                    "amount": 500,
                    "total_price": 500
                },
                {
                    "cloth": "jackets",
                    "quantity": 1,
                    "amount": 700,
                    "total_price": 700
                },
                {
                    "cloth": "shirt",
                    "quantity": 1,
                    "amount": 500,
                    "total_price": 500
                },
                {
                    "cloth": "suit",
                    "quantity": 1,
                    "amount": 5000,
                    "total_price": 5000
                }
            ],
            "order_amount": 6700,
            "date": "2022-06-18 10:57:02",
            "payment_method": "wallet",
            "payment_status": "successful",
            "branch_address": "Calabar Branch",
            "branch_phone": "08074224016",
            "branch_email": "[email protected]",
            "service_name": "Laundry",
            "pickup_address": "No. 30 ofem Ntum Street",
            "pickup_time": "0000-00-00 00:00:00",
            "dropoff_address": "No. 30 ofem Ntum Street",
            "dropoff_time": "0000-00-00 00:00:00",
            "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
            "delivery_status": "PROCCESSING",
            "delivery_method": "Pick-up & Drop-off",
            "delivery_price": "400"
        }
    ]
}

CodePudding user response:

I think, having looked at the response you posted, that the problem seems to be that the response items is added to on each iteration through the nested loop. Perhaps declare the $done array initially within the foreach($result as $key => $value){ loop before entering the inner loops

$result = $query_stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $key => $value){
   
    $items = json_decode($value["items"],true);                           
    $done=array();
    
    foreach( $items as $key1 => $value1 ) {

        $sql = "SELECT price,cloth_id as cloth FROM pricing WHERE `serial` = :serial";
        $stmt = $this->conn->prepare( $sql );
        $stmt->bindValue(':serial', $value1['pair_id'], PDO::PARAM_INT);
        $stmt->execute();
        $price = $stmt->fetch(PDO::FETCH_ASSOC);
        
        $done[]=array(
            'cloth'         =>  $price['cloth'],
            'quantity'      =>  $value1['qty'],
            'amount'        =>  (int)$price['price'],
            'total_price'   =>  (int)$price['price'] * (int)$value1['qty']
        );
        
    }
    $value['items']=$done;
    $value['order_amount'] = (int)$value['order_amount'];
    $resp[]=$value;
}

return [
    "success"   =>  1,
    "data"      =>  $resp
];

CodePudding user response:

the json you provide had success and data columns so your interest is data you should have then access to each row ..if you uncomment print_r line i provide you will see all rows data can you extract $value->... ;In the 1st line you see your json between $result=json_decode(<<<json ... json)->data; witch wil point exact the target to data column you need to extract with foreach

try work around this ..

<?php
    $result=json_decode(<<<json
    
    {
        "success": 1,
        "data": [
            {
                "order_id": "34",
                "order_ref": "DASYXKGD4N",
                "items": [
                    {
                        "cloth": "jeans",
                        "quantity": 10,
                        "amount": 500,
                        "total_price": 5000
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 2,
                        "amount": 500,
                        "total_price": 1000
                    },
                    {
                        "cloth": "suit",
                        "quantity": 5,
                        "amount": 5000,
                        "total_price": 25000
                    }
                ],
                "order_amount": 32200,
                "date": "2022-06-13 12:52:20",
                "payment_method": "wallet",
                "payment_status": "successful",
                "branch_address": "Calabar Branch",
                "branch_phone": "08074224016",
                "branch_email": "[email protected]",
                "service_name": "Laundry",
                "pickup_address": "No. 30 ofem Ntum Street",
                "pickup_time": "0000-00-00 00:00:00",
                "dropoff_address": "No. 30 ofem Ntum Street",
                "dropoff_time": "0000-00-00 00:00:00",
                "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
                "delivery_status": "PROCCESSING",
                "delivery_method": "Pick-up & Drop-off",
                "delivery_price": "400"
            },
            {
                "order_id": "37",
                "order_ref": "DLJLGD80MT",
                "items": [
                    {
                        "cloth": "jeans",
                        "quantity": 10,
                        "amount": 500,
                        "total_price": 5000
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 2,
                        "amount": 500,
                        "total_price": 1000
                    },
                    {
                        "cloth": "suit",
                        "quantity": 5,
                        "amount": 5000,
                        "total_price": 25000
                    },
                    {
                        "cloth": "jeans",
                        "quantity": 10,
                        "amount": 500,
                        "total_price": 5000
                    },
                    {
                        "cloth": "jackets",
                        "quantity": 2,
                        "amount": 700,
                        "total_price": 1400
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 2,
                        "amount": 500,
                        "total_price": 1000
                    },
                    {
                        "cloth": "suit",
                        "quantity": 5,
                        "amount": 5000,
                        "total_price": 25000
                    }
                ],
                "order_amount": 32400,
                "date": "2022-06-17 18:26:07",
                "payment_method": "wallet",
                "payment_status": "successful",
                "branch_address": "Calabar Branch",
                "branch_phone": "08074224016",
                "branch_email": "[email protected]",
                "service_name": "Laundry",
                "pickup_address": "No. 30 ofem Ntum Street",
                "pickup_time": "0000-00-00 00:00:00",
                "dropoff_address": "No. 30 ofem Ntum Street",
                "dropoff_time": "0000-00-00 00:00:00",
                "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
                "delivery_status": "PROCCESSING",
                "delivery_method": "Pick-up & Drop-off",
                "delivery_price": "400"
            },
            {
                "order_id": "38",
                "order_ref": "FAPFCXDV8W",
                "items": [
                    {
                        "cloth": "jeans",
                        "quantity": 10,
                        "amount": 500,
                        "total_price": 5000
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 2,
                        "amount": 500,
                        "total_price": 1000
                    },
                    {
                        "cloth": "suit",
                        "quantity": 5,
                        "amount": 5000,
                        "total_price": 25000
                    },
                    {
                        "cloth": "jeans",
                        "quantity": 10,
                        "amount": 500,
                        "total_price": 5000
                    },
                    {
                        "cloth": "jackets",
                        "quantity": 2,
                        "amount": 700,
                        "total_price": 1400
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 2,
                        "amount": 500,
                        "total_price": 1000
                    },
                    {
                        "cloth": "suit",
                        "quantity": 5,
                        "amount": 5000,
                        "total_price": 25000
                    },
                    {
                        "cloth": "jeans",
                        "quantity": 1,
                        "amount": 500,
                        "total_price": 500
                    },
                    {
                        "cloth": "jackets",
                        "quantity": 1,
                        "amount": 700,
                        "total_price": 700
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 1,
                        "amount": 500,
                        "total_price": 500
                    },
                    {
                        "cloth": "suit",
                        "quantity": 1,
                        "amount": 5000,
                        "total_price": 5000
                    }
                ],
                "order_amount": 6700,
                "date": "2022-06-18 10:55:25",
                "payment_method": "wallet",
                "payment_status": "successful",
                "branch_address": "Calabar Branch",
                "branch_phone": "08074224016",
                "branch_email": "[email protected]",
                "service_name": "Laundry",
                "pickup_address": "No. 30 ofem Ntum Street",
                "pickup_time": "0000-00-00 00:00:00",
                "dropoff_address": "No. 30 ofem Ntum Street",
                "dropoff_time": "0000-00-00 00:00:00",
                "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
                "delivery_status": "PROCCESSING",
                "delivery_method": "Pick-up & Drop-off",
                "delivery_price": "400"
            },
            {
                "order_id": "39",
                "order_ref": "BHWMSLAR1E",
                "items": [
                    {
                        "cloth": "jeans",
                        "quantity": 10,
                        "amount": 500,
                        "total_price": 5000
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 2,
                        "amount": 500,
                        "total_price": 1000
                    },
                    {
                        "cloth": "suit",
                        "quantity": 5,
                        "amount": 5000,
                        "total_price": 25000
                    },
                    {
                        "cloth": "jeans",
                        "quantity": 10,
                        "amount": 500,
                        "total_price": 5000
                    },
                    {
                        "cloth": "jackets",
                        "quantity": 2,
                        "amount": 700,
                        "total_price": 1400
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 2,
                        "amount": 500,
                        "total_price": 1000
                    },
                    {
                        "cloth": "suit",
                        "quantity": 5,
                        "amount": 5000,
                        "total_price": 25000
                    },
                    {
                        "cloth": "jeans",
                        "quantity": 1,
                        "amount": 500,
                        "total_price": 500
                    },
                    {
                        "cloth": "jackets",
                        "quantity": 1,
                        "amount": 700,
                        "total_price": 700
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 1,
                        "amount": 500,
                        "total_price": 500
                    },
                    {
                        "cloth": "suit",
                        "quantity": 1,
                        "amount": 5000,
                        "total_price": 5000
                    },
                    {
                        "cloth": "jeans",
                        "quantity": 1,
                        "amount": 500,
                        "total_price": 500
                    },
                    {
                        "cloth": "jackets",
                        "quantity": 1,
                        "amount": 700,
                        "total_price": 700
                    },
                    {
                        "cloth": "shirt",
                        "quantity": 1,
                        "amount": 500,
                        "total_price": 500
                    },
                    {
                        "cloth": "suit",
                        "quantity": 1,
                        "amount": 5000,
                        "total_price": 5000
                    }
                ],
                "order_amount": 6700,
                "date": "2022-06-18 10:57:02",
                "payment_method": "wallet",
                "payment_status": "successful",
                "branch_address": "Calabar Branch",
                "branch_phone": "08074224016",
                "branch_email": "[email protected]",
                "service_name": "Laundry",
                "pickup_address": "No. 30 ofem Ntum Street",
                "pickup_time": "0000-00-00 00:00:00",
                "dropoff_address": "No. 30 ofem Ntum Street",
                "dropoff_time": "0000-00-00 00:00:00",
                "instruction": "This Cloths Should be washed carefully to avoid washing out of color",
                "delivery_status": "PROCCESSING",
                "delivery_method": "Pick-up & Drop-off",
                "delivery_price": "400"
            }
        ]
    }
    
    json)->data;
    
    foreach($result as $key => $value){
    
        //print_r($value);
        echo 'each order id: '.$value->order_id;
        echo '<hr>';
    }
    ?>
  • Related