使用PHP计算for循环内表中的先前值

时间:2018-03-26 22:47:55

标签: php mysql codeigniter

...编辑 我有一个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。谢谢你迅速回复

2 个答案:

答案 0 :(得分:0)

如果我找对你,那么你可能想做这件事。

$array1 = array (300, 280, 240, 205);
$array2 = array (20, 40, 35, 35);

$length = count($array1);
for ($x = 0; $x < $length; $x++) {
  echo $array1[$x] - $array2[$x];
    echo "<br>";
} 

如果你没有从表中获取数组作为默认数据,你可以简单地将它们传递给数组。

答案 1 :(得分:0)

示例不清楚,但我会根据我的理解尝试解释,我假设我们需要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
相关问题