Last active
August 2, 2024 08:10
-
-
Save vimkaf/767bbf6931cc25ec5662a23184f3e27e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$cart = [ | |
"product_id_3" => 8, | |
"number_of_quantity_3" => 3, | |
"price_3" => 30000, | |
"total_3" => 90000, | |
"product_id_2" => 6, | |
"number_of_quantity_2" => 3, | |
"price_2" => 100, | |
"total_2" => 300, | |
"product_id_1" => 5, | |
"number_of_quantity_1" => 3, | |
"price_1" => 100, | |
"total_1" => 300, | |
"discount" => 0, | |
"grand_total" => 90600 | |
]; | |
//$cart = $_POST; | |
$discount = $cart['discount']; | |
$grandTotal = $cart['grand_total']; | |
unset($cart['discount'], $cart['grand_total']); | |
$keys = array_keys($cart); | |
$ids = []; | |
foreach ($keys as $key) { | |
$seperateKeys = explode("_", $key); | |
$ids[] = $seperateKeys[array_key_last($seperateKeys)]; | |
} | |
$ids = array_unique($ids); | |
$query = "INSERT INTO sales_total(`discount`,`grand_total`) VALUES($discount, $grandTotal)"; | |
echo $query . PHP_EOL; | |
foreach ($ids as $id) { | |
$product_id = $cart["product_id_{$id}"]; | |
$quantity = $cart["number_of_quantity_{$id}"]; | |
$price = $cart["price_{$id}"]; | |
$total = $cart["total_{$id}"]; | |
$query = "INSERT INTO sales(`product_id`,`number`,`price`,`total`) VALUES($product_id, $quantity, $price, $total)"; | |
echo $query . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output:
INSERT INTO sales_total(
discount
,grand_total
) VALUES(0, 90600)INSERT INTO sales(
product_id
,number
,price
,total
) VALUES(8, 3, 30000, 90000)INSERT INTO sales(
product_id
,number
,price
,total
) VALUES(6, 3, 100, 300)INSERT INTO sales(
product_id
,number
,price
,total
) VALUES(5, 3, 100, 300)