title: Cash book record Inventory system
body: <p>I am almost done with a small inventory system but i am having a &quot;concept&quot; problem.</p>
  <p>The problem is with the cash record.</p>
  <p>I have a page to make cash records manually with option of either credit or debit, but mostly this is for credit records as sales will automatically be stored in sales table and also stored cash table as debit.</p>
  <p>so the cash table looks like this:</p>
  <pre><code>  Date       | description   | debit | credit  |
    2014-08-23 | sales         | 456   |         |
    2014-08-23 | transportation|       | 234     |
    2014-08-23 | donation      | 567   |         |
  </code></pre>
  <p>I am using the above table to calculate for profit per month (debit - credit = profit)</p>
  <p>This is all working fine to check for month;y flow of cash.</p>
  <p>The problem is i dont want it to be just for a month (the monthly cash flow is needed though and its fine already).</p>
  <p>With the above i do a query like:</p>
  <pre><code>$date = date(&quot;Y-m&quot;);
  $sql = &quot;SELECT * FROM cash WHERE date LIKE '$date%'&quot;
  </code></pre>
  <p>so this will give me all for that particular month.</p>
  <p>The main problem now is:</p>
  <p>I want the starting cash for the next month to be the profit from the last month (balance brought forward) that will be able to show the profit of the business and not just the profit for a month.</p>
  <p>I cant think of the best way to do this</p>
  <p><strong>Edit (how i am doing it):</strong></p>
  <p>Database:</p>
  <pre><code>CREATE TABLE cash 
  (
   id int auto_increment primary key, 
   date DATE, 
   descs varchar (255),
   debit varchar(30),
   credit varchar (30)
  );
  
  INSERT INTO cash
  (date, descs, debit, credit)
  VALUES
  ('2014-08-24', 'balance b/f', '5000',''),
  ('2014-08-24', 'transportation','', '350'),
  ('2014-08-24', 'sales','2340', '');
  </code></pre>
  <p><strong>My Database query</strong></p>
  <pre><code>$date = date('Y-m');
  
  $sql = &quot;SELECT * FROM cash WHERE date LIKE '$date%'&quot; //this will get all from same year and month
  </code></pre>
  <p><strong>PHP page</strong></p>
  <p>I now have a <code>$cashs</code> with array of the result of the SQL query. then i do</p>
  <pre><code>     &lt;table class=&quot;table table-striped table-hover&quot; id=&quot;table&quot;&gt;
                          &lt;thead&gt;
                            &lt;tr&gt;
                              &lt;th&gt;No/s&lt;/th&gt;
                              &lt;th&gt;Date&lt;/th&gt;
                              &lt;th&gt;Purpose&lt;/th&gt;
                              &lt;th&gt;Debit&lt;/th&gt;
                              &lt;th&gt;Credit&lt;/th&gt;
                            &lt;/tr&gt;
                          &lt;/thead&gt;
                          &lt;tbody&gt;
                          &lt;?php $i=1; $total=0; foreach($cashs AS $cash): ?&gt;
                          &lt;tr&gt;
                            &lt;td&gt;&lt;?php echo $i; ?&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;?php echo $cash-&gt;date; ?&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;?php echo $cash-&gt;desc; ?&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;?php echo $cash-&gt;debit; $total+=$cash-&gt;debit; ?&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;?php echo $cash-&gt;credit; $total-=$cash-&gt;credit; ?&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;?php $i++; endforeach; ?&gt;
                          &lt;tr&gt;
                            &lt;td&gt;&lt;strong&gt;Total&lt;/strong&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;/td&gt;
                            &lt;td&gt;&lt;strong&gt;&lt;?php echo $total; ?&gt;&lt;/strong&gt;&lt;/td&gt;
                          &lt;/tr&gt;
                          &lt;/tbody&gt;
                         &lt;/table&gt;
  </code></pre>
  <p>This is working fine but what i want is a way to get the <code>$total</code> as used in the above code of the previous month also so it will be balance b/f as debit. That should be the starting balance for the new month.</p>
  <p><img src="https://i.stack.imgur.com/vs4sW.png" alt="Picture of result" /></p>