如何减去这个“非最佳实践表”

时间:2012-09-18 21:42:49

标签: php

需要最后一部分的帮助。如何从费用中扣除费用并从中减去,以获得剩余的费用....

收入 - 费用=剩余...你明白了......

猜猜我的mysql表不是最好的方法,但我是一个菜鸟,这就是我想出来的......

// summary income

$query_income = "SELECT type, SUM(january), SUM(february), SUM(march), SUM(april),     SUM(may), SUM(june), SUM(juli), SUM(august), SUM(september), SUM(october), SUM(november), SUM(december) FROM data WHERE year = '2012' AND type = '1'";
$results = mysql_query($query_income);

while($row = mysql_fetch_array( $results )) {

   $total_income = $row['SUM(january)'] + $row['SUM(february)'] + $row['SUM(march)'] + $row['SUM(april)'] + $row['SUM(may)'] + $row['SUM(june)'] + $row['SUM(juli)'] + $row['SUM(august)'] + $row['SUM(september)'] + $row['SUM(october)'] + $row['SUM(november)'] + $row['SUM(december)'];

 echo "<tr>";
  echo "<td><b>Income</b></td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(january)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(february)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(march)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(april)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(may)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(june)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(juli)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(august)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(september)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(october)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(november)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row['SUM(december)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($total_income) ."</td>";
 echo "</tr>";

}

// summary expenses

$query_expense = "SELECT type, SUM(january), SUM(february), SUM(march), SUM(april), SUM(may), SUM(june), SUM(juli), SUM(august), SUM(september), SUM(october), SUM(november), SUM(december) FROM data WHERE year = '2012' AND type = '2'";
  $results = mysql_query($query_expense);

while($row1 = mysql_fetch_array( $results )) {

   $total_expense = $row1['SUM(january)'] + $row1['SUM(february)'] + $row1['SUM(march)'] + $row1['SUM(april)'] + $row1['SUM(may)'] + $row1['SUM(june)'] + $row1['SUM(juli)'] + $row1['SUM(august)'] + $row1['SUM(september)'] + $row1['SUM(october)'] + $row1['SUM(november)'] + $row1['SUM(december)'];


   echo "<tr>";
  echo "<td><b>Expenses</b></td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(january)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(february)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(march)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(april)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(may)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(june)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(juli)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(august)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(september)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(october)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(november)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($row1['SUM(december)']) ."</td>";
  echo "<td style='text-align:right'>" . number_format($total_expense) ."</td>";
 echo "</tr>";

}

// summary, whats left

   echo "<tr>";
  echo "<td><b>Leftover</b></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
  echo "<td style='text-align:right'></td>";
 echo "</tr>";

2 个答案:

答案 0 :(得分:1)

您可以在一个查询中执行此操作:

SELECT type, SUM(january) AS `jan`, SUM(february) AS `feb`, ...
FROM data
WHERE `year` = '2012'
GROUP BY type
ORDER BY type ASC

然后处理结果如:

$totals = array();

while($row = mysql_fetch_array( $results )) {
   $type = 'unknown';
   switch ((integer)$row['type']) {
       1:
           $type = 'income';
           break;
       2:
           $type = 'expense';
           break;
   }
   unset($row['type']);
   $totals[$type] = $row;
}

$total_income = array_sum($totals['income']);
$total_expense = array_sum($totals['expenses']);

现在你有一个多维数组,你可以很容易地使用它来总结整个arr

答案 1 :(得分:0)

这样的事情怎么样?

SELECT 
    income.1 - expense.1 AS 1,
    income.2 - expense.2 AS 2,
    income.3 - expense.3 AS 3,
    income.4 - expense.4 AS 4,
    income.5 - expense.5 AS 5,
    income.6 - expense.6 AS 6,
    income.7 - expense.7 AS 7,
    income.8 - expense.8 AS 8,
    income.9 - expense.9 AS 9,
    income.10 - expense.10 AS 10,
    income.11 - expense.11 AS 11,
    income.12 - expense.12 AS 12
FROM
    (SELECT
        SUM(january) AS 1, 
        SUM(february) AS 2,  
        SUM(march) AS 3,  
        SUM(april) AS 4,  
        SUM(may) AS 5,  
        SUM(june) AS 6,  
        SUM(juli) AS 7,  
        SUM(august) AS 8,  
        SUM(september) AS 9, 
        SUM(october) AS 10, 
        SUM(november) AS 11, 
        SUM(december) AS 12
    FROM data 
    WHERE year = '2012' AND type = '1') income,
    (SELECT type  AS 1,  
        SUM(january) AS 1, 
        SUM(february) AS 2,  
        SUM(march) AS 3,  
        SUM(april) AS 4,  
        SUM(may) AS 5,  
        SUM(june) AS 6,  
        SUM(juli) AS 7,  
        SUM(august) AS 8,  
        SUM(september) AS 9, 
        SUM(october) AS 10, 
        SUM(november) AS 11, 
        SUM(december) AS 12
    FROM data 
    WHERE year = '2012' AND type = '2') expense