简体   繁体   English

多个帐户的捐款总额(变量)

[英]Sum donations (variables) for several accounts

-- Table structure for table 'members'
CREATE TABLE `members` (
`mID` INTEGER NOT NULL AUTO_INCREMENT, 
`fname` VARCHAR(50), 
`lname` VARCHAR(50), 
`status` VARCHAR(20), 
`dateregistered` VARCHAR(255), 
INDEX (`lname`), 
PRIMARY KEY (`mID`)
) ENGINE=myisam DEFAULT CHARSET=utf8;

-- Table structure for table 'payments'
CREATE TABLE `payments` (
`paymentsID` INTEGER NOT NULL AUTO_INCREMENT, 
`amount` INTEGER, 
`type` VARCHAR(255), 
`officer` VARCHAR(255), 
`dayentered` DATETIME, 
`mID` INTEGER, 
INDEX (`mID`), 
PRIMARY KEY (`paymentsID`)
) ENGINE=myisam DEFAULT CHARSET=utf8;

I have the above two tables in my database. 我的数据库中有上述两个表。 The database keeps information on donations that are categorised according to their account type (for utility bills, building projects etc) and members donate directly to a particular account of their choice (they can donate to one account or to all account or any number of accounts). 该数据库保留根据其帐户类型分类的捐赠信息(用于公用事业账单,建筑项目等),而会员直接捐赠给他们选择的特定账户(他们可以捐赠给一个账户或所有账户或任意数量的账户) )。

The database was working fine in MS Access but now I have to migrate it to MySQL and PHP and that's where my problem lies. 该数据库在MS Access中运行良好,但是现在我必须将其迁移到MySQL和PHP,这就是我的问题所在。 I can not manage to come up with the code to list the names of the members and for each member show the total amount donated for each account per month as well as a running total for the year. 我无法提供列出成员名称的代码,并且为每个成员显示每个帐户每个月的捐赠总额以及该年度的运行总额。

Please help with the code. 请帮助代码。 Thanks. 谢谢。

Not as fast/efficient as a pure SQL solution, but this would do the job. 速度不如纯SQL解决方案快/高效,但这可以完成工作。

$query = "
SELECT 
  `members`.`mID`,
  `members`.`fname`, 
  `members`.`lname`, 
  `payments`.`amount`, 
  `payments`.`type`, 
  `payments`.`dayentered`
FROM `members` JOIN `payments` 
ON `members`.`mID` = `payments`.`mID`";
$result = mysqli($link, $query);
$results = array();
while($row=mysqli_fetch_assoc($result)) {

  $mID    = $row['mID'];
  $type   = $row['type'];
  $amount = (int) $row['amount'];
  $date   = $row['dayentered'];
  $year   = date('Y', strtotime($date));

  if(!array_key_exists($mid, $results)) 
    $results[$mid] = array($row['fname'], $row['lname']);

  if(!array_key_exists($date, $results[$mid])) 
    $results[$mid][$date] = array();

  if(!array_key_exists($type, $results[$mid][$date])) 
    $results[$mid][$date][$type] = $amount;
  else
    $results[$mid][$date][$type] += $amount;

  if(!array_key_exists($year, $results[$mid]))
    $results[$mid][$year] = $amount;
  else
    $results[$mid][$year] += $amount;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM