Home > database >  Dynamic array into a static array
Dynamic array into a static array

Time:09-30

I have this static code today that works fine, now i need to make the order line dynamic. The code is from a e-commerce store.

$order = [
    "purchase_country" => "SE",
    "purchase_currency" => "SEK",
    "locale" => "sv-SE",
    "order_amount" => 10000,
    "order_tax_amount" => 2000,
    "order_lines" => [
        [
            "type" => "physical",
            "reference" => "123050",
            "name" => "Tomatoes",
            "quantity" => 10,
            "quantity_unit" => "kg",
            "unit_price" => 600,
            "tax_rate" => 2500,
            "total_amount" => 6000,
            "total_tax_amount" => 1200
        ],
        [
            "type" => "physical",
            "reference" => "543670",
            "name" => "Bananas",
            "quantity" => 1,
            "quantity_unit" => "bag",
            "unit_price" => 5000,
            "tax_rate" => 2500,
            "total_amount" => 4000,
            "total_discount_amount" => 1000,
            "total_tax_amount" => 800
        ]
    ],

    "merchant_urls" => [
        "terms" => "https://www.example.com/villkor.php",
        "cancellation_terms" => "https://www.example.com/terms/cancellation.html",
        "checkout" => "https://www.example.com/_script/checkout.php",
        "confirmation" => "https://www.example.com/_script/checkout.php",

        // Callbacks
        "push" => "https://www.example.com/api/push",
        "validation" => "https://www.example.com/api/validation",
        "shipping_option_update" => "https://www.example.com/api/shipment",
        "address_update" => "https://www.example.com/api/address",
        "notification" => "https://www.example.com/api/pending",
        "country_change" => "https://www.example.com/api/country"
    ]
];

Now I need to make order_lines dynamic.

I have tried this code:

$order = [
"purchase_country" => "SE",
"purchase_currency" => "SEK",
"locale" => "sv-SE",
"order_amount" => 10000,
"order_tax_amount" => 2000,
"order_lines" => [

$cartsql="select a, b, c, d from lux_cart WHERE status = 0 ORDER BY a DESC";    
if ($result=mysqli_query($mysqli,$cartsql)){
    while ($cartrow=mysqli_fetch_row($result)){
        
        $itemid = $cartrow[0];
        $prodTitel = $cartrow[1];
        $antalcart = $cartrow[2];
        $prodPris = $cartrow[3];
        
        [
            "type" => "physical",
            "reference" => "$itemid",
            "name" => "$prodTitel",
            "quantity" => $antalcart,
            "unit_price" => $prodPris,
            "tax_rate" => 2500,
            "total_amount" => $prodPris,
            "total_tax_amount" => 1200
        ],
                                


}
}

"merchant_urls" => [
    "terms" => "https://www.example.com/villkor.php",
    "cancellation_terms" => "https://www.example.com/terms/cancellation.html",
    "checkout" => "https://www.example.com/_script/checkout.php",
    "confirmation" => "https://www.example.com/_script/checkout.php",

    // Callbacks
    "push" => "https://www.example.com/api/push",
    "validation" => "https://www.example.com/api/validation",
    "shipping_option_update" => "https://www.example.com/api/shipment",
    "address_update" => "https://www.example.com/api/address",
    "notification" => "https://www.example.com/api/pending",
    "country_change" => "https://www.example.com/api/country"
]
];

But the page broken without any error message. I think I need to make some array ore something to resolve this issue. Can someone help me figure out what I am doing wrong here?

CodePudding user response:

You can't execute PHP code inside an array definition like that. That's causing a parse error that's the reason your page is broken with no error. (You can change settings on your development server to show these errors as well.) Instead, define the array with an empty array for order_lines

$order = [
   "purchase_country" => "SE",
   "purchase_currency" => "SEK",
   "locale" => "sv-SE",
   "order_amount" => 10000,
   "order_tax_amount" => 2000,
   "order_lines" => [],

   "merchant_urls" => [
      "terms" => "https://www.example.com/villkor.php",
      "cancellation_terms" => "https://www.example.com/terms/cancellation.html",
      "checkout" => "https://www.example.com/_script/checkout.php",
      "confirmation" => "https://www.example.com/_script/checkout.php",

      // Callbacks
      "push" => "https://www.example.com/api/push",
      "validation" => "https://www.example.com/api/validation",
      "shipping_option_update" => "https://www.example.com/api/shipment",
      "address_update" => "https://www.example.com/api/address",
      "notification" => "https://www.example.com/api/pending",
      "country_change" => "https://www.example.com/api/country"
   ]
];

Then fill that key with values from your query.

$cartsql="select a, b, c, d from lux_cart WHERE status = 0 ORDER BY a DESC";    
if ($result=mysqli_query($mysqli,$cartsql)){
    while ($cartrow=mysqli_fetch_row($result)){
        
        $itemid = $cartrow[0];
        $prodTitel = $cartrow[1];
        $antalcart = $cartrow[2];
        $prodPris = $cartrow[3];
        

        // Use [] to append the rows to the existing key
        $order['order_lines'][] = [
            "type" => "physical",
            "reference" => "$itemid",
            "name" => "$prodTitel",
            "quantity" => $antalcart,
            "unit_price" => $prodPris,
            "tax_rate" => 2500,
            "total_amount" => $prodPris,
            "total_tax_amount" => 1200
        ];
    }
}
  • Related