我的问题很简单,但我是以cakephp开头的。我会解释我的问题:如何使用foreach生成表格(表格)
我有2个表:
Table: Expenditures
Columns:
sub_category_id
account_id
date
ammount
created
modified
description
Table: BudgetRecords
Columns:
budget_id
ammount
sub_category_id
created
modified
我有一个“家庭作业”的事,和我的老师希望我做一个报告。此报告将生成一个表格,并显示余额(Expenditures.ammount - BudgetRecords.ammount)和每个月(BudgetRecords.created或Expenditures.date,无论如何)。
这里是我的问题:我试图做一个foreach,每年传递(如果存在),如果一年存在,然后执行每个月的查询。但是,我如何使用CakePHP来做到这一点?我试图只在控制器内部做到这一点,它运行良好。但是,只能回到去年的最后一个月。然后,我尝试了一种不同的方法。
在视图内部,执行查询并搜索是否存在年份。如果存在,则搜索本月。然后把结果给我。
这就是我所做的:
里面的景观:
<table align="center" border="2" rules="All">
<tr>
<td>Valor:</td>
<td>Data:</td>
</tr>
<?php
for ($month = 1; $month <= 12; $month++) {
if (strlen($month) == 1)
$month = '0' . $month;
foreach ($Expenditure->SaldoMes($month) as $result) {
echo "<tr>";
echo "<td> R$:" . $result[0] . "</td>";
echo "<td> " . $result[1] . "</td>";
echo "</tr>";
}
}
?>
</table>
内置控制器:
public function SaldoMes($month = null) {
$month = 0;
for ($month = 1; $month <= 12; $month++) {
if (strlen($month) == 1)
$month = '0' . $month;
$query = "SELECT (SUM(ex.ammount) - SUM(br.ammount)) as total , br.created as data
FROM budget_Records br, expenditure ex
where SUBSTRING(br.created, 6, 2) = '" . $month .
"' and SUBSTRING(br.created, 6, 2) = SUBSTRING(ex.created, 6, 2)";
$this->set('Expenditure', $this->Expenditure->query($query));
foreach ($this->Expenditure->query($query) as $records) {
$this->set(('Total'), $records[0]['total']);
$this->set(('Data'), $records['br']['data']);
}
}
}
我希望我已经解释了我的问题不够好,有人告诉我一个办法。
我已经说过,在我的问题的描述。我知道如何使用模型,控制器和视图,但我不知道如何调用Controller内部的方法来搜索一年,一个月并构建表。 –
我需要在foreach中调用此方法,并且在该方法内部,将在其分配中接收一个值,并按月/年执行查询。 –
好的,也许你正在寻找更多这样的。 http://book.cakephp.org/1.2/view/434/requestAction – earlonrails