title: Is it possible to add or subtract from value of previous loop?
body: <p>I have a table called <code>item_movement</code> wherein I store number of inventory purchased, sold, returned by customer or returned to supplier:</p>
  
  <pre><code>------------------------------------------------------------------------
  | 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     |
  ------------------------------------------------------------------------
  </code></pre>
  
  <p>The inventory_id is the identification of each of the inventory.</p>
  
  <p>My code is:</p>
  
  <pre><code>$conn = new mysqli('localhost', 'username', 'password', 'database');
  $query = $_GET['id'];
  $item_stock = $conn-&gt;query("SELECT * FROM item_movement WHERE `inventory_id` = '%".$query."%' ORDER BY date DESC") or die(mysql_error());
  
  echo '&lt;table class="stock-info-table"&gt;
            &lt;tr class="center"&gt;
              &lt;th&gt;Date&lt;/th&gt;
              &lt;th&gt;Transaction Type&lt;/th&gt;
              &lt;th&gt;Quantity&lt;/th&gt;
              &lt;th&gt;Running Stock&lt;/th&gt;
            &lt;/tr&gt;';
  while($row = mysqli_fetch_array($item_stock)){
          echo '&lt;tr&gt;
            &lt;td&gt;'.$row['date'].'&lt;/td&gt;
            &lt;td&gt;'.$row['trans_type'].'&lt;/td&gt;
            &lt;td&gt;'.$row['quantity'].'&lt;/td&gt;
            &lt;td&gt;RUNNING STOCK HERE&lt;/td&gt;
          &lt;/tr&gt;';
  }
  echo '&lt;/table&gt;';
  </code></pre>
  
  <p><strong>Problem:</strong>
  I haven't figured out how to show the running stock, as you can see in my code. The running stock column should show the number of stock movement per transaction like if the purchase quantity for inventory ID 1 is 10, then it should show 10. If there's a sale quantity of 2, then it should show 8 (10-2). Basically the formula for the stock is: <em>Purchase - Sale + Customer Return - Supplier Pullout</em>.
  The data should also show the recent transactions first (DESC), in which the sequence of the running stock should also be based.</p>
  
  <p><em>Edit:</em>
  Here's an example data I would like to show for an inventory_id:</p>
  
  <pre><code>-------------------------------------------------------------------
  |    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        |
  -------------------------------------------------------------------
  </code></pre>
  
  <p>Let me know if this is possible.</p>