gpt4 book ai didi

php - 使用 PHP 的 for 循环内的表中的先前值进行计算

转载 作者:行者123 更新时间:2023-11-29 17:49:38 25 4
gpt4 key购买 nike

已编辑...我有一个 HTML 表,它从数据库中的不同表获取它的值(输出)(使用 Join 语句)。随着循环的进行,我想要实现的是使用该行的先前值来生成新值。让我们使用两列为例,假设我们有循环中有 5 行

我希望我的 Total_Qty 随着循环的进行而减少,即 Total_Qty - Shipped_Qty,因此下一行的新 Total_Qty 将是例如

300 - 50 = 250,
then the next will be
250 -25 = 225, and
225 - 100 = 115 and lastly
115 - 80 = 35

我的真实输出

Date Order|  Item Name|  Total_Qty} Shipped_Qty| Unit_Price|    Amount| Item_Balance
24th March| 25X25 1/2mm | 300 | 50 | | 5000 | 3250000 | 250
24th March| 25X25 1/2mm | 300 | 25 | | 5000 | 3375000 | 275
27th March| 25X25 1/2mm | 300 | 100| | 5000 | 3000000 | 200
27th March| 25X25 1/2mm | 300 | 80 | | 5000 | 310000000 | 220

我的模型

function get_join_item_vendor1($vendor_name, $item_name){         
$this->db->select('shipments.date_created as dateCreated, shipments.id as ship_id, shipments.*, accounts.*, items.*, customers.* , vendors.*, shipments.current_quantity AS ship_current_quantity, remaining_stocks.quantities AS currentQuantity, shipments.*, store_stocks.*' );
$this->db->from('shipments');
$this->db->join('accounts', 'accounts.id = shipments.rep_id', 'left');
$this->db->join('items', 'items.id = shipments.item_id', 'left');
$this->db->join('customers', 'customers.id = shipments.customer_id', 'left');
$this->db->join('vendors', 'vendors.id = shipments.vendor_id', 'left');
$this->db->join('remaining_stocks', 'remaining_stocks.item_id = shipments.item_id', 'left');
$this->db->join('store_stocks', 'store_stocks.item_id = shipments.item_id', 'left');
$this->db->where('items.id', $item_name);
$this->db->where('vendors.vendor_name', $vendor_name);
$this->db->group_by('shipments.shipped_quantity');
$this->db->order_by('shipments.id');
$query = $this->db->get();
return $query->result();
}

我的查看文件

<table class="table table-striped">
<thead>
<tr>
<th>Date Order</th>
<th>Item Name</th>
<th>Total Item Qty</th>
<th>Shipped Item Qty</th>
<th>Unit Price</th>
<th>Amount</th>
<th>Balance</th>
</tr>
</thead>
<tbody>
<?php
foreach ($stocks as $stock) {?>
<tr>
<td><?= date('jS F Y',$stock->dateCreated) ?></td>
<td><?=$stock->item_name ?></td>
<td><?=$stock->quantities ?></td>
<td><?=$stock->shipped_quantity ?></td>
<td><?=$stock->unit_price ?></td>
<td><?='₦'.number_format(($stock->unit_price * $total_quantity->total_qty) - ($stock->unit_price * $stock->shipped_quantity) , 2) ?></td>
<td> <?= $stock->quantities - $stock->shipped_quantity; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

请提供任何帮助,我们将不胜感激。希望我的插图现在更清楚了。

谢谢

PS:我正在使用 codeigniter。感谢您的快速回复

最佳答案

这个例子不清楚,但我会尝试根据我的理解进行解释,我假设我们需要 v1 和 v2 来计算新的 v1 并且你没有办法通过索引访问数组。而不是使用之前的迭代值来计算该值。我将使用当前迭代值计算下一个 v1。

$result = mysqli_query($connection,"Your query");
$next_v1 = false;
while($row = mysqli_fetch_row($result)){
if($next_v1 === false )
$next_v1 = $row[0]; // 300 in our case
$v2 = $row[1]; // 20
print($next_v1, $v2);// custom function that will print the row;
$next_v1 = $next_v1 - $v2; // now next value is 280
}

更新一个

根据您的情况,您可以创建一个新变量来存储累积的发货项目。

<table class="table table-striped">
<thead>
<tr>
<th>Date Order</th>
<th>Item Name</th>
<th>Total Item Qty</th>
<th>Shipped Item Qty</th>
<th>Unit Price</th>
<th>Amount</th>
<th>Balance</th>
</tr>
</thead>
<tbody>
<?php
$shipped_accumulator = 0;
foreach ($stocks as $stock) {?>
<tr>
<td><?= date('jS F Y',$stock->dateCreated) ?></td>
<td><?=$stock->item_name ?></td>
<td><?=$stock->quantities - $shipped_accumulator ?></td>
<td><?=$stock->shipped_quantity ?></td>
<td><?=$stock->unit_price ?></td>
<td><?='₦'.number_format(($stock->unit_price * $total_quantity->total_qty) - ($stock->unit_price * $stock->shipped_quantity) , 2) ?></td>
<td> <?= $stock->quantities - $stock->shipped_quantity; ?></td>
</tr>
<?php
$shipped_accumulator += $stock->shipped_quantity;
} ?>
</tbody>
</table>

所以这个表会是这样的

Date Order | Total_Qty          | shipped_accumulator   | Shipped_Qty   | Unit_Price    |    Amount | Item_Balance
24th March | 300 - 0 = 300 | 0 | 50 | 5000 | 3250000 | 300 - 50 - 0 = 250
24th March | 300 - 50 =250 | 50 | 25 | 5000 | 3375000 | 300 - 25 - 50 = 225
27th March | 300 - 75 = 225 | 75 | 100 | 5000 | 3000000 | 300 - 100 - 75 = 125
27th March | 300 - 175 = 125 | 175 | 80 | 5000 | 310000000 | 300 - 80 - 175 = 45

关于php - 使用 PHP 的 for 循环内的表中的先前值进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49501828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com