title: PHP/MySQL Multidimensional/Associative Arrays 3 Trial Balance
body: <p>I'm currently working on an accounting code that will print out a Trial Balance based on data stored in MySql. I can't seem to get my head around the arrays that should get the trial balance displayed correctly. The data-set looks somewhat like this:</p>
  
  <p>id | &nbsp; &nbsp; account &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;amount &nbsp; &nbsp;    | &nbsp; &nbsp; &nbsp; &nbsp;paymode &nbsp; &nbsp; <br/>
  1   &nbsp; &nbsp; &nbsp; TUITION   &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;5000      &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Cheque &nbsp;<br/>
  2    &nbsp; &nbsp;&nbsp; &nbsp;TUITION     &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; 2000      &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cash &nbsp;&nbsp;<br/>
  3    &nbsp; &nbsp;&nbsp; &nbsp;Repairs     &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; 500       &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cash&nbsp;&nbsp;<br/><br/></p>
  
  <p>The output of the trial balance should display in double entry format like this:</p>
  
  <p>ACCOUNT &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;DEBIT &nbsp; &nbsp;    | &nbsp; &nbsp; &nbsp; &nbsp;CREDIT &nbsp; &nbsp; <br/>
  Bank &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5000<br/>
  Cash &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2000 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 500<br/>
  TUITION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7000 &nbsp; &nbsp;<br/>
  Repairs &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500 &nbsp; &nbsp;<br/></p>
  
  <p>Please note above that two accounts(Bank and Cash) have to be generated automatically by code depending on the paymode field...and their values(5000 and 2000 in this case) are also dependent on the values found under the "amount" column.</p>
  
  <p>So my current code looks like this so far: (It outputs accounts in their total but only on the DEBIT side)</p>
  
  <pre><code>// Make the query(Get Accounts and Totals):
  $rein = @mysqli_query ($con, "SELECT account, amount, paymode FROM tbl_finance"); // Run the query.
  
  $bg = '#eeeeee';
  $accarr = array();
  while($rowin = mysqli_fetch_array($rein, MYSQLI_ASSOC)) {
      if(!isset($accarr[$rowin['type']])) {
      $accarr[$rowin['type']]['amount'] = 0;
  }
  $sum = $rowin['amount_paid'];
  $accarr[$rowin['type']]['amount'] += $sum;
  $accarr[$rowin['type']]['paymode'] = $rowin['payterm'];
  } // End While
  mysqli_free_result($rein);
  print_r($accarr);
  
  echo '
  &lt;table&gt;
  &lt;tr&gt;
      &lt;td align="left"&gt;&lt;b&gt;ACCOUNT&lt;/b&gt;&lt;/td&gt;
      &lt;td align="right"&gt;&lt;b&gt;DEBIT&lt;/b&gt;&lt;/td&gt;
      &lt;td align="right"&gt;&lt;b&gt;CREDIT&lt;/b&gt;&lt;/td&gt;
  &lt;/tr&gt;
  ';
  
  $bg = '#eeeeee';
  foreach($accarr as $acc =&gt; $data) {
  $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
  
  echo '
  &lt;tr bgcolor="'. $bg .'"&gt;
      &lt;td align="left"&gt;'. $acc .'&lt;/td&gt;
          &lt;td align="right"&gt;'. $data['amount'] .'&lt;/td&gt;
  &lt;/tr&gt;
  ';
  } // End foreach
  
  echo '&lt;/table&gt;';
  </code></pre>
  
  <p>Edit: The current output of above statement:</p>
  
  <p>ACCOUNT &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;DEBIT &nbsp; &nbsp;    | &nbsp; &nbsp; &nbsp; &nbsp;CREDIT &nbsp; &nbsp; <br/>
  TUITION &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 7000 &nbsp; &nbsp;<br/>
  Repairs &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;500 &nbsp; &nbsp;<br/></p>
  
  <p>Question: How can the data-set above be used to output data as displayed above(aligned to DEBIT and CREDIT where appropriate)?</p>
  
  <p>Thanks in Advance</p>