php - 是否可以从上一个循环的值中添加或减去?

标签 php mysql while-loop

我有一个名为 item_movement 的表,其中我存储购买、销售、客户退回或退回给供应商的库存数量:

------------------------------------------------------------------------
| trans_id |    date     |   trans_type     | inventory_id | quantity  |
------------------------------------------------------------------------
|     1    | 2016-07-26  | Purchase         |      1       |    10     |
------------------------------------------------------------------------
|     2    | 2016-07-26  | Purchase         |      2       |     8     |
------------------------------------------------------------------------
|     3    | 2016-07-27  | Sale             |      1       |     2     |
------------------------------------------------------------------------
|     4    | 2016-07-28  | Customer Return  |      1       |     1     |
------------------------------------------------------------------------
|     5    | 2016-07-29  | Supplier Pullout |      2       |     5     |
------------------------------------------------------------------------

inventory_id 是每个库存的标识。

我的代码是:

$conn = new mysqli('localhost', 'username', 'password', 'database');
$query = $_GET['id'];
$item_stock = $conn->query("SELECT * FROM item_movement WHERE `inventory_id` = '%".$query."%' ORDER BY date DESC") or die(mysql_error());

echo '<table class="stock-info-table">
          <tr class="center">
            <th>Date</th>
            <th>Transaction Type</th>
            <th>Quantity</th>
            <th>Running Stock</th>
          </tr>';
while($row = mysqli_fetch_array($item_stock)){
        echo '<tr>
          <td>'.$row['date'].'</td>
          <td>'.$row['trans_type'].'</td>
          <td>'.$row['quantity'].'</td>
          <td>RUNNING STOCK HERE</td>
        </tr>';
}
echo '</table>';

问题: 正如您在我的代码中看到的那样,我还没有想出如何显示正在运行的库存。运行库存列应显示每次交易的库存移动数量,如果库存 ID 1 的采购数量为 10,则应显示 10。如果销售数量为 2,则应显示 8 (10-2)。库存的基本公式是:采购 - 销售 + 客户返回 - 供应商撤出。 数据还应首先显示最近的交易(DESC),其中流水库存的顺序也应基于此。

编辑: 这是我想为 inventory_id 显示的示例数据:

-------------------------------------------------------------------
|    Date     |   Transaction Type   |  Quantity  | Running Stock |
-------------------------------------------------------------------
|  2016-07-29 |   Purchase           |     5      |     12        |
-------------------------------------------------------------------
|  2016-07-28 |   Supplier Pullout   |     2      |      7        |
-------------------------------------------------------------------
|  2016-07-27 |   Customer Return    |     1      |      9        |
-------------------------------------------------------------------
|  2016-07-26 |   Sale               |     2      |      8        |
-------------------------------------------------------------------
|  2016-07-25 |   Purchase           |     10     |     10        |
-------------------------------------------------------------------

如果可行,请告诉我。

最佳答案

您可以通过在一个变量中保存您的库存总量来做到这一点。

我假设您在某处持有当前库存,但出于演示目的,此示例将从零开始值。

还可以使用 mysqli_fetch_assoc(),因为它只返回一个列的关联数组,而不是一个数值数组。

$cur_stock = 0;

$id= $_GET['id'];
$item_stock = $conn->query("SELECT * 
                            FROM `item_movement` 
                            WHERE `inventory_id` = $id 
                            ORDER BY date ASC") 
              or die(mysql_error());

echo '<table class="stock-info-table">
          <tr class="center">
            <th>Date</th>
            <th>Transaction Type</th>
            <th>Quantity</th>
            <th>Running Stock</th>
          </tr>';

while($row = $conn->fetch_assoc($item_stock)){

    switch ($row['trans_type']) {
        case 'Purchase' :
        case 'Customer Return' :
            $cur_stock += (int)$row['quantity'];
            break;

        case 'Sale' :
        case 'Supplier Pullout' :
            $cur_stock -= (int)$row['quantity'];
            break;
    }

    echo '<tr>
            <td>'.$row['date'].'</td>
           <td>'.$row['trans_type'].'</td>
            <td>'.$row['quantity'].'</td>
            <td>' . $cur_stock . '</td>
          </tr>';
}
echo '</table>';

It would be better to use prepared statement and parameterized statements

一个例子是

$cur_stock = 0;

$id= $_GET['id'];

$stmt = $conn->prepare("SELECT * 
                         FROM `item_movement` 
                         WHERE `inventory_id` = ? 
                         ORDER BY date ASC"); 
if ( $stmt === false ) {
   echo $conn->error;
   exit;
}
$stmt->bind_param('i', $id);

$status = $stmt->execute();
if ( $status === false ) {
   echo $conn->error;
   exit;
}

$result = $stmt->get_result();

while($row = $result->fetch_assoc($item_stock)){

    switch ($row['trans_type']) {
        case 'Purchase' :
        case 'Customer Return' :
            $cur_stock += (int)$row['quantity'];
            break;

        case 'Sale' :
        case 'Supplier Pullout' :
            $cur_stock -= (int)$row['quantity'];
            break;
    }

    echo '<tr>
            <td>'.$row['date'].'</td>
           <td>'.$row['trans_type'].'</td>
            <td>'.$row['quantity'].'</td>
            <td>' . $cur_stock '</td>
          </tr>';
}
echo '</table>';

获得所需输出(即反转)的最简单方法是将结果临时存储在数组中,然后在输出 HTML 表格之前反转数组。

$temp = array();
while($row = $result->fetch_assoc($item_stock)){

    switch ($row['trans_type']) {
        case 'Purchase' :
        case 'Customer Return' :
            $cur_stock += (int)$row['quantity'];
            break;

        case 'Sale' :
        case 'Supplier Pullout' :
            $cur_stock -= (int)$row['quantity'];
            break;
    }
    $row['running_stock'] = $cur_stock;
    $temp[] = $row;
}
// reverse the array
$temp2 = array_reverse($temp);

foreach($temp2 as $row) {
    echo '<tr>
            <td>' . $row['date']          .'</td>
            <td>' . $row['trans_type']    .'</td>
            <td>' . $row['quantity']      .'</td>
            <td>' . $row['running_stock'] .'</td>
          </tr>';
}
echo '</table>';

关于php - 是否可以从上一个循环的值中添加或减去?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38715773/

相关文章:

javascript - ajax登录表单不起作用

php - 将时间从mysql插入日期表行

php - 数组键索引附带数字

php - PHP 中的 DOMDocument 和 dom 模块

PHP继续导致无限循环

php - 必须有一个更简单的方法..从 mysql 中提取数据

python - 在 Python 中区分空行和文件结尾

c - 为什么“while(!feof(file))”总是错误的?

php - 将 PEM 格式的公钥传递给 openssl_pkey_get_public 会出现错误 :0906D06C:PEM routines:PEM_read_bio:no start line

php - MySQL 出现重复更新错误