я пытаюсь получить общее количество
The data from array would look like this;
[desc] [quantity]
[foo1], [ 2 ]
[foo2], [ 2 ]
....
Это "код":
function saveInvoiceDetail(array $wreceipt_items, $warehouse = '', $tracking = ''){
global $con;
$y = 0;
foreach ($wreceipt_items as $wreceipt_item){
$desc = mysqli_real_escape_string( $con, trim( $wreceipt_item['desc'] ) );
$quantity = mysqli_real_escape_string( $con, trim( $wreceipt_item['quantity'] ) );
$query = "INSERT INTO wreceipt_items (`id`, `warehouse`, `desc`, `quantity`)
VALUES (NULL, '$warehouse', '$desc', '$quantity')";
$y++;
mysqli_query($con, $query);
}
$quantity_sum = array_sum($wreceipt_item['quantity'])
$x = $quantity_sum; //repeat this insert ($quantity_sum) times
for ($i = 1; $i <= $x; $i++){
$query2 = "INSERT INTO control (`id`, `warehouse`, `tracking`)
VALUES ('', '$warehouse-$i', '$tracking')";
mysqli_query($con, $query2);
}
}
Желаемый результат: выполнить '$ query2' X раз, где X = $quantity_sum;Это возможно?Я не могу понять.
Примечание: 'foreach' & 'for' работает, но я не могу использовать 'for' без 'количественной суммы'
EDIT - ИСПРАВЛЕНО
function saveInvoiceDetail(array $wreceipt_items, $warehouse = '', $tracking = ''){
global $con;
$y = 0;
foreach ($wreceipt_items as $wreceipt_item){
$desc = mysqli_real_escape_string( $con, trim( $wreceipt_item['desc'] ) );
$quantity = mysqli_real_escape_string( $con, trim( $wreceipt_item['quantity'] ) );
$query = "INSERT INTO wreceipt_items (`id`, `warehouse`, `desc`, `quantity`)
VALUES (NULL, '$warehouse', '$desc', '$quantity')";
$y++;
mysqli_query($con, $query);
}
$quantity_sum = array_sum(array_column($wreceipt_items, 'quantity'));
$x = $quantity_sum; //repeat this insert ($quantity_sum) times
for ($i = 1; $i <= $x; $i++){
$query2 = "INSERT INTO control (`id`, `warehouse`, `tracking`)
VALUES ('', '$warehouse-$i', '$tracking')";
mysqli_query($con, $query2);
}
}