2012-05-02 7 views
0

私の問題は簡単ですが、私はcakephpで始まります。私が行うには「ホームワーク」を持っているforeachを使ってグリッド(表)を生成する方法

Table: Expenditures 
Columns: 
sub_category_id 
account_id 
date  
ammount  
created  
modified  
description 

Table: BudgetRecords 
Columns: 
budget_id 
ammount  
sub_category_id 
created  
modified 

、と先生は私がレポートをしたい:私は2つのテーブルを持っている

:私は私の問題を説明します。このレポートはテーブルを生成し、残高(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.. 
    } 
} 

「これらのアクションのためのビューファイルは、アプリ/ビュー/レシピ/ view.ctp、アプリ/ビュー/レシピ/ share.ctp、およびアプリ/ビュー/レシピ/ search.ctpだろう。従来のビューファイル名は、アクション名の大文字と小文字の下位バージョンです。

したがって、SaldoMes()のような別のメソッドを使用したい場合は、単にviewメソッドで呼び出すだけです。解決

+0

私は既に私の問題の説明でそれを言っています。 Model、Controller、Viewの使い方はちょっと分かりますが、コントローラ内でメソッドを呼び出す方法がわからないので、年と月を検索してテーブルを作成します。 –

+0

私はforeach内でこのメソッドを呼び出す必要があり、そのメソッド内では、代入者に値を受け取り、月/年ごとにクエリを実行します。 –

+0

あなたはこれ以上のように探しています。 http://book.cakephp.org/1.2/view/434/requestAction – earlonrails

関連する問題