如何使用foreach生成网格(表)

时间:2012-05-02 04:25:21

标签: foreach html-table cakephp-2.0

我的问题很简单,但我从cakephp开始。我会解释我的问题:

我有两张桌子:

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做到的?我试图只在Controller内部这样做,它运行正常。但仅在去年的最后一个月归还给我。然后,我尝试了不同的方法。

在视图内部,执行查询并搜索是否存在年份。如果存在,则搜索本月。然后给我回复结果。

这就是我所做的:

在视图内:

<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']);
        }
    }
}

我希望我能够很好地解释我的问题,以便有人向我展示解决方案。

2 个答案:

答案 0 :(得分:2)

我认为这会有所帮助 http://book.cakephp.org/2.0/en/controllers.html

这部分是我想要的。取自该网站。

<?php
# /app/Controller/RecipesController.php

class RecipesController extends AppController {
    public function view($id) {
        //action logic goes here..
    }

    public function share($customer_id, $recipe_id) {
        //action logic goes here..
    }

    public function search($query) {
        //action logic goes here..
    }
}

“这些操作的视图文件是app / View / Recipes / view.ctp,app / View / Recipes / share.ctp和app / View / Recipes / search.ctp。传统的视图文件名是操作名称的下限和下划线版本。“

因此,如果你想使用另一种方法,比如SaldoMes(),那么只需在view方法中调用它。

答案 1 :(得分:0)

@earlonrails,已解决:

public function SaldoMes() {
    $result = array();
    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) {
            $result[$month][0] = $records[0]['total'];
            $result[$month][1] = $records['br']['data'];
        }
    }
    return $this->set('result', $result);
}

使用$ result数组,现在我可以得到我的值。

感谢您的帮助。