2012-07-19 71 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可以让你的生活变得更轻松。仅在视图中“展示”事物也是一种好的做法。在视图中查找数据库应该不鼓励。