2012-05-02 36 views
0

我的问题很简单,但我是以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']); 
     } 
    } 
} 

我希望我已经解释了我的问题不够好,有人告诉我一个办法。

回答

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

使用$结果数组,我可以让我的价值观。

感谢您的帮助。

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.ctp,应用程序/查看/食谱/ share.ctp和应用程序/查看/食谱/ search.ctp。传统观点文件名称是操作名称的下部和下划线版本。“

所以,如果你想只使用另一种方法像SaldoMes()那么只需在视图方法中调用它。

+0

我已经说过,在我的问题的描述。我知道如何使用模型,控制器和视图,但我不知道如何调用Controller内部的方法来搜索一年,一个月并构建表。 –

+0

我需要在foreach中调用此方法,并且在该方法内部,将在其分配中接收一个值,并按月/年执行查询。 –

+0

好的,也许你正在寻找更多这样的。 http://book.cakephp.org/1.2/view/434/requestAction – earlonrails