2012-07-19 101 views
0

我正在處理一個小應用程序,並且我的controllers_viewpvcontroller有問題。我正在嘗試顯示報告。用戶可以決定年份和月份,並在提交之後,報告將顯示在表格下的同一頁面中。控制器錯誤 - Zend

這裏是我的控制器:

require_once ('library/Zend/Controller/Action.php'); 

class controllers_viewpvController extends Zend_Controller_Action { 

public function init() 
{ 

} 

public function viewpvAction() 
{ 
    require_once 'models/PastPV.php'; 

    $data = $this->getRequest()->getParams(); 
    $year = $data['year']; 
    $month = $data['month']; 
    $pv = new PastPV(); 
    $return = $pv->viewPV($year, $month); // this function is working fine. 
    $this->view->assign('values',$return); 

} 

public function getpvAction() 
{ 


} 

} 

這裏是我的viewpv.phtml

<body> 

<h1 id="h2"> Review</h1> 

<br><div><center><form action="../Viewpv/viewpv" method="post" > 

<h3>Payment Vouchers for : <select name="month"> 
      <option value="null">(Month)</option> 
    <option value="01">January</option> 
    <option value="02">February</option> 
    <option value="03">March</option> 
    <option value="04">April</option> 
    <option value="05">May</option> 
    </select> 

<select name="year"> 
<option value="null">(Year)</option> 
<option value="2010">2010</option> 
<option value="2011">2011</option> 
<option value="2012">2012</option>                    
</select></h3> 
<br><br> 
<input type="submit" value="View"> 

</form></center> 
</div></body> 

<!--the codes to display the form are below--> 

<?php 
$return = $this->values; 
if(!empty($return)) 
$tbl = '<table><tr><td>Document No</td></tr>'; 
while($data = $return->fetchRow()){  //here I think 'fetchRow()' may cause a problem. 
               //but that is not my question. 
$tbl.='<tr><td>'.$data['docNo'].'</td></tr>'; 
} 
$tbl.='</table>'; 
echo $tbl; 
?> 

當我嘗試加載網頁,它提供了以下PHP錯誤:

Undefined index: year 
Undefined index: month 

另外,它給出Zend_Db_Statement_Exception,No parameters were bound。我的感受是,我沒有找到正確的方法來做到這一點。

請幫我這個。如果有更好的方法來實現這一點,請讓我知道。

+0

而不是'getParams()'嘗試'getPost()'。如果那有效,那麼你是否有適當的路由規則可能會干擾'getParams()'? – JaredMcAteer 2012-07-19 15:49:07

+0

什麼是您用於訪問此頁面的完整URL? – tubaguy50035 2012-07-19 16:32:29

+0

@ tubaguy50035 - 'Viewpv/viewpv'就是我正在使用的。我使用這個代碼'url(array("controller" => "Viewpv", "action" => "viewpv"));?>> View' – 2012-07-19 17:02:02

回答

1

如果表單與您要顯示的報告位於同一頁面上,則需要檢查POST數據。你可以通過在Zend中使用請求對象來做到這一點。

if($this->getRequest()->isPost()) 
{ 
    //Process the form and get things you need to display the report 
} 

目前,如果你直接去該頁面像它聽起來就像你在做什麼,你要查找尚未被提交回動作指數。

因此,這將失敗,從而導致你的錯誤:

$year = $data['year']; 
$month = $data['month']; 

那麼你就對信息的數據庫查詢,造成數據庫錯誤。如果你添加我列出的「if」陳述,你應該沒問題。

您應該閱讀Zend文檔Zend FormThe Request Object。 Zend Form可以讓你的生活變得更輕鬆。僅在視圖中「展示」事物也是一種好的做法。在視圖中查找數據庫應該不鼓勵。